逻辑与证明¶
Logic and Proof¶
1.介绍¶
1. Introduction¶
1.1.数学证明¶
1.1. Mathematical Proof¶
尽管有书面证据表明早在公元前 3000 年埃及就出现了数学活动,但许多学者认为真正的数学诞生于公元前六世纪左右的古希腊,当时首次引入了演绎证明。亚里士多德认为米利都的泰勒斯不仅认识到了我们所知道的东西的重要性,而且认识到了我们如何知道的重要性,并在演绎方法中找到了知识的基础。公元前 300 年左右,欧几里得在他的论文《几何原本》中编纂了几何学的演绎方法。几个世纪以来,欧几里得的公理化风格被视为严格论证的典范,不仅在数学中,而且在哲学和科学中也是如此。
Although there is written evidence of mathematical activity in Egypt as early as 3000 BC, many scholars locate the birth of mathematics proper in ancient Greece around the sixth century BC, when deductive proof was first introduced. Aristotle credited Thales of Miletus with recognizing the importance of not just what we know but how we know it, and finding grounds for knowledge in the deductive method. Around 300 BC, Euclid codified a deductive approach to geometry in his treatise, the Elements. Through the centuries, Euclid’s axiomatic style was held as a paradigm of rigorous argumentation, not just in mathematics, but in philosophy and the sciences as well.
这是一个用当代数学语言来表达的普通证明的例子。它证实了毕达哥拉斯学派所知道的一个事实。
Here is an example of an ordinary proof, in contemporary mathematical language. It establishes a fact that was known to the Pythagoreans.
定理。
Theorem.
证明。假设
Proof. Suppose
最后一个等式意味着
The last equation implies that
事实是
The fact that
在下一个例子中,我们关注自然数,
In the next example, we focus on the natural numbers,
自然数
A natural number
定理。每个大于或等于 2 的自然数都可以写成素数的乘积。
Theorem. Every natural number greater than or equal to 2 can be written as a product of primes.
证明。我们通过归纳法进行
Proof. We proceed by induction on
根据需要,素数的乘积。
a product of primes, as required.
稍后,我们将看到更多的事实是真实的:每一个大于 2 的自然数都可以用独特的方式写成素数的乘积,这一事实被称为算术基本定理。
Later, we will see that more is true: every natural number greater than 2 can be written as a product of primes in a unique way, a fact known as the fundamental theorem of arithmetic.
本课程的首要目标是教您编写清晰易读的数学证明。我们将通过考虑大量示例来实现这一目标,同时也采取反思的观点:我们将仔细研究数学语言的组成部分和数学证明的结构,以便更好地理解它们的工作原理。
The first goal of this course is to teach you to write clear, readable mathematical proofs. We will do this by considering a number of examples, but also by taking a reflective point of view: we will carefully study the components of mathematical language and the structure of mathematical proofs, in order to gain a better understanding of how they work.
1.2.符号逻辑¶
1.2. Symbolic Logic¶
为了理解证明的工作原理,研究“符号逻辑”这一主题将大有裨益,它提供了数学语言和证明的理想模型。在《前分析篇》中,这位古希腊哲学家着手分析推理模式,并发展了三段论理论。以下是三段论的一个例子:
Toward understanding how proofs work, it will be helpful to study a subject known as “symbolic logic,” which provides an idealized model of mathematical language and proof. In the Prior Analytics, the ancient Greek philosopher set out to analyze patterns of reasoning, and developed the theory of the syllogism. Here is one instance of a syllogism:
每个人都是动物。
Every man is an animal.
任何动物都会死。
Every animal is mortal.
因此,每个人都终有一死。
Therefore every man is mortal.
亚里士多德指出,这种推论的正确性与个别陈述的真实性或虚假性无关,而是与一般模式有关:
Aristotle observed that the correctness of this inference has nothing to do with the truth or falsity of the individual statements, but, rather, the general pattern:
每个 A 都是 B。
Every A is B.
每个 B 都是 C。
Every B is C.
因此每个 A 都是 C。
Therefore every A is C.
我们可以用各种属性来代替 A、B 和 C;试着用鱼、独角兽、游泳生物、神话生物等属性来代替。由此产生的各种陈述可能为真或为假,但所有实例都具有以下关键特征:如果两个假设为真,则结论也为真。我们通过说推理有效来表达这一点。
We can substitute various properties for A, B, and C; try substituting the properties of being a fish, being a unicorn, being a swimming creature, being a mythical creature, etc. The various statements that result may come out true or false, but all the instantiations will have the following crucial feature: if the two hypotheses come out true, then the conclusion comes out true as well. We express this by saying that the inference is valid.
尽管亚里士多德的推理理论所针对的语言模式有限,但我们仍要感谢他提出的一个关键洞见:我们可以根据逻辑形式对有效的推理模式进行分类,同时抽象出具体内容。这一基本观察是整个符号逻辑领域的基础。
Although the patterns of language addressed by Aristotle’s theory of reasoning are limited, we have him to thank for a crucial insight: we can classify valid patterns of inference by their logical form, while abstracting away specific content. It is this fundamental observation that underlies the entire field of symbolic logic.
17 世纪,莱布尼茨提出了“普遍性”的设计,这是一种通用的符号语言,人们可以用它精确地表达任何断言,以及“推理演算”,这是一种“思维演算”,可以表达精确的推理规则。莱布尼茨自己也采取了一些措施来开发这种语言和演算,但更大的进步是在 19 世纪取得的,这要归功于布尔、弗雷格、皮尔斯、施罗德等人的努力。20 世纪初,这些努力在数理逻辑领域蓬勃发展。
In the seventeenth century, Leibniz proposed the design of a characteristica universalis, a universal symbolic language in which one would express any assertion in a precise way, and a calculus ratiocinatur, a “calculus of thought” which would express the precise rules of reasoning. Leibniz himself took some steps to develop such a language and calculus, but much greater strides were made in the nineteenth century, through the work of Boole, Frege, Peirce, Schroeder, and others. Early in the twentieth century, these efforts blossomed into the field of mathematical logic.
如果您考虑上一节中的证明示例,您会注意到一些术语和推理规则特定于当前主题,与数字以及质数、合数、偶数、奇数等属性有关。但还有其他术语和推理规则并非特定于领域,例如与“每个”、“一些”、“和”和“如果……那么”等词相关的术语和规则。符号逻辑的目标是识别这些推理和论证的核心要素并解释它们的工作原理,以及解释如何引入和使用更多特定领域的概念。
If you consider the examples of proofs in the last section, you will notice that some terms and rules of inference are specific to the subject matter at hand, having to do with numbers and the properties of being prime, composite, even, odd, and so on. But there are other terms and rules of inference that are not domain specific, such as those related to the words “every,” “some,” “and,” and “if … then.” The goal of symbolic logic is to identify these core elements of reasoning and argumentation and explain how they work, as well as to explain how more domain-specific notions are introduced and used.
为此,我们将引入关键逻辑概念的符号,包括以下内容:
To that end, we will introduce symbols for key logical notions, including the following:
,“ ” , “ ” ,“ ” , “ ” ,“ ” , “ ” ,“ ” , “ ” ,“ ” , “ ” ,“ ” , “ ”
然后,我们将提供一个正式的证明系统,让我们通过演绎的方式证明这些陈述之间的某些蕴涵是有效的。
We will then provide a formal proof system that will let us establish, deductively, that certain entailments between such statements are valid.
我们将使用的证明系统是自然演绎法的一种,这是 Gerhard Gentzen 在 20 世纪 30 年代引入的一种证明系统,用于模拟非正式论证风格。在这个系统中,判断的基本单位是断言一个陈述,
The proof system we will use is a version of natural deduction, a type of proof system introduced by Gerhard Gentzen in the 1930s to model informal styles of argument. In this system, the fundamental unit of judgment is the assertion that a statement,
这应该被理解为:假设
This should be interpreted as saying: assuming
我们将看到,人们可以更紧凑地写出这样的证明,而隐含假设,这样上面的规则就可以表达如下:
We will see that one can write such proofs more compactly leaving the hypotheses implicit, so that the rule above is expressed as follows:
在这种格式中,上一节中第一个证明的片段可能呈现如下:
In this format, a snippet of the first proof in the previous section might be rendered as follows:
此类证明的复杂性很快就会变得无法控制,即使是基本数学事实的完整证明也会变得相当冗长。此类系统并非为编写严肃的数学而设计。相反,它们提供了理想化的数学推理模型,只要它们捕捉到某种非正式证明的结构,它们就使我们能够研究数学推理的性质。
The complexity of such proofs can quickly grow out of hand, and complete proofs of even elementary mathematical facts can become quite long. Such systems are not designed for writing serious mathematics. Rather, they provide idealized models of mathematical inference, and insofar as they capture something of the structure of an informal proof, they enable us to study the properties of mathematical reasoning.
本课程的第二个目标是帮助您理解自然演绎,作为正式演绎系统的一个示例。
The second goal of this course is to help you understand natural deduction, as an example of a formal deductive system.
1.3.交互式定理证明¶
1.3. Interactive Theorem Proving¶
数理逻辑的早期研究旨在表明,至少在原则上,普通数学论证可以用符号演算建模。如上所述,复杂性问题限制了实践中可以实现的范围;即使是基本的数学论证也需要很长的推导过程,这些推导过程很难写,也很难读,而且对促进对底层数学的理解几乎没有帮助。
Early work in mathematical logic aimed to show that ordinary mathematical arguments could be modeled in symbolic calculi, at least in principle. As noted above, complexity issues limit the range of what can be accomplished in practice; even elementary mathematical arguments require long derivations that are hard to write and hard to read, and do little to promote understanding of the underlying mathematics.
然而,自 20 世纪末以来,计算证明助手的出现开始使完全形式化成为可能。通过与定理证明软件交互,用户可以构建复杂定理的形式化推导,这些推导可以由计算机存储和检查。自动化方法可用于手动填补小空白、公理化地验证长计算或确定性地填充长推理链。然而,自动化的范围目前相当有限。交互式定理证明中使用的策略是要求用户提供足够的信息,以便系统能够构建和检查形式化推导。这通常涉及用一种为此目的而设计的“编程语言”编写证明。例如,这是Lean定理证明器中的一个简短证明:
Since the end of the twentieth century, however, the advent of computational proof assistants has begun to make complete formalization feasible. Working interactively with theorem proving software, users can construct formal derivations of complex theorems that can be stored and checked by computer. Automated methods can be used to fill in small gaps by hand, verify long calculations axiomatically, or fill in long chains of inferences deterministically. The reach of automation is currently fairly limited, however. The strategy used in interactive theorem proving is to ask users to provide just enough information for the system to be able to construct and check a formal derivation. This typically involves writing proofs in a sort of “programming language” that is designed with that purpose in mind. For example, here is a short proof in the Lean theorem prover:
section
variables (P Q : Prop)
theorem my_theorem : P ∧ Q → Q ∧ P :=
assume h : P ∧ Q,
have P, from and.left h,
have Q, from and.right h,
show Q ∧ P, from and.intro ‹Q› ‹P›
end
section
variables (P Q : Prop)
theorem my_theorem : P ∧ Q → Q ∧ P :=
assume h : P ∧ Q,
have P, from and.left h,
have Q, from and.right h,
show Q ∧ P, from and.intro ‹Q› ‹P›
end
如果您以在线形式阅读当前文本,您会在正式的“证明脚本”上方看到一个按钮,上面写着“试试看!”按下该按钮会在编辑器窗口中打开证明,并在浏览器中运行 Lean 版本来处理证明,将其转换为公理推导,并验证其正确性。您可以通过改变编辑器中的文本进行实验;任何错误都会在右侧窗口中注明。
If you are reading the present text in online form, you will find a button above the formal “proof script” that says “try it!” Pressing the button opens the proof in an editor window and runs a version of Lean inside your browser to process the proof, turn it into an axiomatic derivation, and verify its correctness. You can experiment by varying the text in the editor; any errors will be noted in the window to the right.
Lean 中的证明可以访问先前的数学结果库,所有结果都经过公理基础验证。交互式定理证明领域的一个目标是达到任何当代定理都可以通过这种方式验证的程度。例如,下面是一个正式证明,证明二的平方根是无理数,遵循上面提出的非正式证明的模型:
Proofs in Lean can access a library of prior mathematical results, all verified down to axiomatic foundations. A goal of the field of interactive theorem proving is to reach the point where any contemporary theorem can be verified in this way. For example, here is a formal proof that the square root of two is irrational, following the model of the informal proof presented above:
import data.nat.prime
open nat
theorem sqrt_two_irrational {a b : ℕ} (co : gcd a b = 1) :
a^2 ≠ 2 * b^2 :=
assume h : a^2 = 2 * b^2,
have 2 ∣ a^2,
by simp [h],
have 2 ∣ a,
from prime.dvd_of_dvd_pow prime_two this,
exists.elim this $
assume (c : nat) (aeq : a = 2 * c),
have 2 * (2 * c^2) = 2 * b^2,
by simp [eq.symm h, aeq];
simp [pow_succ', mul_comm, mul_assoc, mul_left_comm],
have 2 * c^2 = b^2,
from mul_left_cancel' dec_trivial this,
have 2 ∣ b^2,
by simp [eq.symm this],
have 2 ∣ b,
from prime.dvd_of_dvd_pow prime_two this,
have 2 ∣ gcd a b,
from dvd_gcd ‹2 ∣ a› ‹2 ∣ b›,
have 2 ∣ (1 : ℕ),
by simp * at *,
show false, from absurd ‹2 ∣ 1› dec_trivial
import data.nat.prime
open nat
theorem sqrt_two_irrational {a b : ℕ} (co : gcd a b = 1) :
a^2 ≠ 2 * b^2 :=
assume h : a^2 = 2 * b^2,
have 2 ∣ a^2,
by simp [h],
have 2 ∣ a,
from prime.dvd_of_dvd_pow prime_two this,
exists.elim this $
assume (c : nat) (aeq : a = 2 * c),
have 2 * (2 * c^2) = 2 * b^2,
by simp [eq.symm h, aeq];
simp [pow_succ', mul_comm, mul_assoc, mul_left_comm],
have 2 * c^2 = b^2,
from mul_left_cancel' dec_trivial this,
have 2 ∣ b^2,
by simp [eq.symm this],
have 2 ∣ b,
from prime.dvd_of_dvd_pow prime_two this,
have 2 ∣ gcd a b,
from dvd_gcd ‹2 ∣ a› ‹2 ∣ b›,
have 2 ∣ (1 : ℕ),
by simp * at *,
show false, from absurd ‹2 ∣ 1› dec_trivial
本课程的第三个目标是教您用精益语言编写基本证明。我们要求您在精益语言中证明的事实将比我们要求您编写的非正式证明更基本,但我们的目的是正式证明将模拟和阐明我们将教给您的非正式证明策略。
The third goal of this course is to teach you to write elementary proofs in Lean. The facts that we will ask you to prove in Lean will be more elementary than the informal proofs we will ask you to write, but our intent is that formal proofs will model and clarify the informal proof strategies we will teach you.
1.4.语义观点¶
1.4. The Semantic Point of View¶
正如我们在此介绍的主题,符号逻辑的目标是指定一种语言和推理规则,使我们能够以可靠的方式获得真相。这个想法是,我们选择的符号表示具有固定含义的对象和概念,我们采用的推理规则使我们能够从真实的假设中得出真实的结论。
As we have presented the subject here, the goal of symbolic logic is to specify a language and rules of inference that enable us to get at the truth in a reliable way. The idea is that the symbols we choose denote objects and concepts that have a fixed meaning, and the rules of inference we adopt enable us to draw true conclusions from true hypotheses.
然而,人们可以采用另一种观点来看待逻辑,即逻辑是一个系统,其中某些符号具有固定的含义,例如“与”、“或”和“非”的符号,而其他符号的含义可以变化。例如,表达式
One can adopt another view of logic, however, as a system where some symbols have a fixed meaning, such as the symbols for “and,” “or,” and “not,” and others have a meaning that is taken to vary. For example, the expression
从这个角度来看,逻辑与其说是一种断言真理的语言,不如说是一种描述可能事态的语言。换句话说,逻辑提供了一种规范语言,表达式可以是真或假,这取决于我们如何解释允许变化的符号。例如,如果我们固定了基本谓词的含义,那么“两个蓝色块之间有一个红色块”这个陈述在给定的块“世界”中可能是真或假,我们可以用这个表达式来描述它为真的世界集。这种逻辑观在计算机科学中很重要,我们使用逻辑表达式从数据库中选择符合某些标准的条目,指定硬件和软件系统的属性,或者断言我们希望约束求解器满足的约束。
From this perspective, logic is not so much a language for asserting truth, but a language for describing possible states of affairs. In other words, logic provides a specification language, with expressions that can be true or false depending on how we interpret the symbols that are allowed to vary. For example, if we fix the meaning of the basic predicates, the statement “there is a red block between two blue blocks” may be true or false of a given “world” of blocks, and we can take the expression to describe the set of worlds in which it is true. Such a view of logic is important in computer science, where we use logical expressions to select entries from a database matching certain criteria, to specify properties of hardware and software systems, or to assert constraints that we would like a constraint solver to satisfy.
一方面,句法 / 演绎观点与另一方面的语义 / 模型理论观点之间存在重要联系。我们将在此过程中探索其中的一些联系。例如,我们将看到,可以将“有效”断言视为在所有可能的非固定符号解释下都为真的断言,将“有效”推论视为在所有可能的状态和事务中都保持真实的推论。从这个角度来看,演绎系统应该只允许我们推导出有效的断言和蕴涵,这种属性称为健全性。如果演绎系统足够强大,可以让我们验证所有有效的断言和蕴涵,则称其为完备的。
There are important connections between the syntactic / deductive point of view on the one hand, and the semantic / model-theoretic point of view on the other. We will explore some of these along the way. For example, we will see that it is possible to view the “valid” assertions as those that are true under all possible interpretations of the non-fixed symbols, and the “valid” inferences as those that maintain truth in all possible states and affairs. From this point of view, a deductive system should only allow us to derive valid assertions and entailments, a property known as soundness. If a deductive system is strong enough to allow us to verify all valid assertions and entailments, it is said to be complete.
本课程的第四个目标是传达逻辑的语义观点,并引导您了解如何使用逻辑表达式来指定事态。
The fourth goal of this course is to convey the semantic view of logic, and to lead you to understand how logical expressions can be used to specify states of affairs.
1.5.目标总结¶
1.5. Goals Summarized¶
总而言之,本课程的目标是:
To summarize, these are the goals of this course:
您应该学会写出清晰的、“有文化的”数学证明。
You should learn to write clear, “literate,” mathematical proofs.
您应该熟悉符号逻辑和演绎证明的形式建模。
You should become comfortable with symbolic logic and the formal modeling of deductive proof.
您应该学习如何使用交互式校对助手。
You should learn how to use an interactive proof assistant.
您应该了解如何使用逻辑作为一种精确的语言来对对象系统及其之间的关系提出主张,并指定某些事态。
You should understand how to use logic as a precise language for making claims about systems of objects and the relationships between them, and specifying certain states of affairs.
让我们花点时间考虑一下这些目标之间的关系。重要的是不要混淆前三个目标。我们处理的是三种数学语言:普通数学语言、数学逻辑的符号表示和交互式证明助手中的计算实现。这些都是非常不同的东西!
Let us take a moment to consider the relationship between some of these goals. It is important not to confuse the first three. We are dealing with three kinds of mathematical language: ordinary mathematical language, the symbolic representations of mathematical logic, and computational implementations in interactive proof assistants. These are very different things!
符号逻辑并非旨在取代普通数学语言,你不应该使用如下符号
Symbolic logic is not meant to replace ordinary mathematical language, and you should not use symbols like
交互式定理证明器使用的证明语言介于两个极端之间。一方面,它们必须具有足够的精度,以便计算机能够处理它们并做出适当的反应;另一方面,它们旨在捕捉非正式语言的一些高级细微差别和特征,以便我们能够编写更复杂的论证和证明。它们植根于符号逻辑,并在设计时考虑到了普通数学语言,旨在弥合两者之间的差距。
The proof languages used by interactive theorem provers lie somewhere between the two extremes. On the one hand, they have to be specified with enough precision for a computer to process them and act appropriately; on the other hand, they aim to capture some of the higher-level nuances and features of informal language in a way that enables us to write more complex arguments and proofs. Rooted in symbolic logic and designed with ordinary mathematical language in mind, they aim to bridge the gap between the two.
本书还旨在向您展示数学是如何从基本概念构建起来的。逻辑提供了游戏规则,然后我们从集合、关系、函数和自然数的性质逐步发展到初等数论、组合学和实数的性质。最后一章以对公理基础的讨论结束了这个故事。
This book also aims to show you how mathematics is built up from fundamental concepts. Logic provides the rules of the game, and then we work our way up from properties of sets, relations, functions, and the natural numbers to elementary number theory, combinatorics, and properties of the real numbers. The last chapter rounds out the story with a discussion of axiomatic foundations.
1.6.关于本教材¶
1.6. About this Textbook¶
这本在线教科书和Lean定理证明器都是新项目,正在进行中。您可以从其项目页面、Leann社区页面和在线教科书《Lean 中的定理证明》了解有关 Lean 的更多信息。
Both this online textbook and the Lean theorem prover are new and ongoing projects. You can learn more about Lean from its project page, the Leann community pages, and the online textbook, Theorem Proving in Lean.
我们非常感谢许多人的反馈和更正,包括 Bruno Cuconato、William DeMeo、Tobias Grosser、Lyle Kopnicky、Alexandre Rademaker、Matt Rice 和 Jason Siefken。
We are grateful for feedback and corrections from a number of people, including Bruno Cuconato, William DeMeo, Tobias Grosser, Lyle Kopnicky, Alexandre Rademaker, Matt Rice, and Jason Siefken.
2.命题逻辑¶
2. Propositional Logic¶
2.1.谜题¶
2.1. A Puzzle¶
下面的谜题名为“恶意与爱丽丝”,出自乔治·J·萨默斯的《逻辑推理谜题》。
The following puzzle, titled “Malice and Alice,” is from George J. Summers’ Logical Deduction Puzzles.
爱丽丝、爱丽丝的丈夫、他们的儿子、他们的女儿以及爱丽丝的兄弟卷入了一起谋杀案。五人中的一人杀死了另外四人中的一人。以下事实涉及上述五个人:
Alice, Alice’s husband, their son, their daughter, and Alice’s brother were involved in a murder. One of the five killed one of the other four. The following facts refer to the five people mentioned:
谋杀案发生时,一名男子和一名女子正一起在酒吧里。
A man and a woman were together in a bar at the time of the murder.
案发时,受害者和凶手正一起在海滩上。
The victim and the killer were together on a beach at the time of the murder.
谋杀案发生时,爱丽丝的两个孩子中有一个是独自一人。
One of Alice’s two children was alone at the time of the murder.
谋杀案发生时,爱丽丝和她的丈夫并不在一起。
Alice and her husband were not together at the time of the murder.
受害者的双胞胎姐妹并不是凶手。
The victim’s twin was not the killer.
凶手比受害者年轻。
The killer was younger than the victim.
这五个人中哪一个是受害者?
Which one of the five was the victim?
花点时间尝试找出解决方案。(你应该假设受害者的双胞胎是上述五个人之一。)萨默斯的书提供了以下提示:“首先找到谋杀案发生时两对人的位置,然后确定凶手和受害者是谁,这样就不会出现矛盾的情况。”
Take some time to try to work out a solution. (You should assume that the victim’s twin is one of the five people mentioned.) Summers’ book offers the following hint: “First find the locations of two pairs of people at the time of the murder, and then determine who the killer and the victim were so that no condition is contradicted.”
2.2.解决方案¶
2.2. A Solution¶
如果您曾经解答过这个谜题,那么您可能已经注意到一些事情。首先,绘制图表并系统地寻找答案是有帮助的。角色、位置和属性的数量是有限的,因此需要考虑的可能“事态”只有有限个。这些数字也足够小,因此系统地搜索所有可能性虽然很乏味,但最终会让您找到正确答案。这是这类逻辑谜题的一个特殊功能;例如,您不会期望通过遍历所有可能性来证明每个大于 2 的偶数都可以写成素数之和。
If you have worked on the puzzle, you may have noticed a few things. First, it is helpful to draw a diagram, and to be systematic about searching for an answer. The number of characters, locations, and attributes is finite, so that there are only finitely many possible “states of affairs” that need to be considered. The numbers are also small enough so that systematic search through all the possibilities, though tedious, will eventually get you to the right answer. This is a special feature of logic puzzles like this; you would not expect to show, for example, that every even number greater than two can be written as a sum of primes by running through all the possibilities.
您可能还注意到,这个问题似乎预设了这个问题有一个唯一的答案,也就是说,在所有符合条件的情况中,只有一个人可能是凶手。先验地,如果没有这个假设,找到某个可能是受害者的人和证明这个人一定是受害者之间是有区别的。换句话说,展示符合条件的情况和最终证明没有其他解决方案之间是有区别的。
Another thing that you may have noticed is that the question seems to presuppose that there is a unique answer to the question, which is to say, over all the states of affairs that meet the list of conditions, there is only one person who can possibly be the killer. A priori, without that assumption, there is a difference between finding some person who could have been the victim and showing that that person had to be the victim. In other words, there is a difference between exhibiting some state of affairs that meets the criteria and demonstrating conclusively that no other solution is possible.
书中公布的解决方案不仅产生了符合标准的情况,而且同时证明了这是唯一符合标准的情况。全文引述如下。
The published solution in the book not only produces a state of affairs that meets the criterion, but at the same time proves that this is the only one that does so. It is quoted below, in full.
从(1)、(2)、(3)中,五个人的角色分别是:酒吧里的男人和女人,海滩上的杀手和受害者,以及单独的孩子。
From (1), (2), and (3), the roles of the five people were as follows: Man and Woman in the bar, Killer and Victim on the beach, and Child alone.
然后,从 (4) 来看,要么爱丽丝的丈夫在酒吧而爱丽丝在海滩上,要么爱丽丝在酒吧而爱丽丝的丈夫在海滩上。
Then, from (4), either Alice’s husband was in the bar and Alice was on the beach, or Alice was in the bar and Alice’s husband was on the beach.
如果爱丽丝的丈夫在酒吧里,和他在一起的女人是他的女儿,独自一人的孩子是他的儿子,而爱丽丝和她的兄弟在海滩上。那么爱丽丝或她的兄弟就是受害者;所以另一个就是凶手。但是,从(5)来看,受害者有一个双胞胎,而这个双胞胎是无辜的。由于爱丽丝和她的兄弟只能是彼此的双胞胎,这种情况是不可能的。因此爱丽丝的丈夫不在酒吧里。
If Alice’s husband was in the bar, the woman he was with was his daughter, the child who was alone was his son, and Alice and her brother were on the beach. Then either Alice or her brother was the victim; so the other was the killer. But, from (5), the victim had a twin, and this twin was innocent. Since Alice and her brother could only be twins to each other, this situation is impossible. Therefore Alice’s husband was not in the bar.
所以 Alice 在酒吧里。如果 Alice 在酒吧里,那她一定是和她的兄弟或儿子在一起的。
So Alice was in the bar. If Alice was in the bar, she was with her brother or her son.
如果爱丽丝和她哥哥在一起,那么她的丈夫就会带着两个孩子中的一个在海滩上。从(5)来看,受害者不可能是她的丈夫,因为其他人都不可能是他的双胞胎;所以凶手是她的丈夫,受害者是他身边的孩子。但这种情况是不可能的,因为它与(6)相矛盾。因此,爱丽丝没有和她的哥哥一起在酒吧里。
If Alice was with her brother, her husband was on the beach with one of the two children. From (5), the victim could not be her husband, because none of the others could be his twin; so the killer was her husband and the victim was the child he was with. But this situation is impossible, because it contradicts (6). Therefore, Alice was not with her brother in the bar.
所以爱丽丝和她的儿子在酒吧里。那么那个独自一人的孩子就是她的女儿。因此,爱丽丝的丈夫和爱丽丝的兄弟一起在海滩上。从之前的推理来看,受害者不可能是爱丽丝的丈夫。但受害者可能是爱丽丝的兄弟,因为爱丽丝可能是他的双胞胎。
So Alice was with her son in the bar. Then the child who was alone was her daughter. Therefore, Alice’s husband was with Alice’s brother on the beach. From previous reasoning, the victim could not be Alice’s husband. But the victim could be Alice’s brother because Alice could be his twin.
所以爱丽丝的兄弟是受害者,而爱丽丝的丈夫是凶手。
So Alice’s brother was the victim and Alice’s husband was the killer.
这一论证依赖于一些“逻辑之外”的因素,例如,父亲不可能比孩子年轻,父母和孩子不可能是双胞胎。但这一论证还涉及许多常见的逻辑术语和相关的推理模式。在下一节中,我们将重点讨论上述论证中出现的一些关键逻辑术语,如“和”、“或”、“不”和“如果……那么”等词。
This argument relies on some “extralogical” elements, for example, that a father cannot be younger than his child, and that a parent and his or her child cannot be twins. But the argument also involves a number of common logical terms and associated patterns of inference. In the next section, we will focus on some of the key logical terms occurring in the argument above, words like “and,” “or,” “not,” and “if … then.”
我们的目标是解释控制这些术语使用的推理模式。为此,我们将使用符号逻辑的方法引入变量
Our goal is to give an account of the patterns of inference that govern the use of those terms. To that end, using the methods of symbolic logic, we will introduce variables
表示该语句
indicates that statement
2.3.推理规则¶
2.3. Rules of Inference¶
2.3.1.含义¶
2.3.1. Implication¶
我们将要讨论的第一个推理模式涉及“如果……那么……”结构,可能很难辨别。它的使用在很大程度上隐含在上述解决方案中。第四段中的推理更详细地阐述如下:
The first pattern of inference we will discuss, involving the “if … then …” construct, can be hard to discern. Its use is largely implicit in the solution above. The inference in the fourth paragraph, spelled out in greater detail, runs as follows:
如果爱丽丝在酒吧,那么爱丽丝就和她的兄弟或儿子在一起。
If Alice was in the bar, Alice was with her brother or her son.
爱丽丝在酒吧里。
Alice was in the bar.
爱丽丝和她的兄弟或儿子在一起。
Alice was with her brother or son.
这条规则有时被称为肯定前件法,即“蕴涵消除法”,因为它告诉我们如何在论证中使用蕴涵。一般来说,它表达如下:
This rule is sometimes known as modus ponens, or “implication elimination,” since it tells us how to use an implication in an argument. As a rule, it is expressed as follows:
读这句话的意思是,如果你有证据
Read this as saying that if you have a proof of
推导“如果……那么”陈述的规则更加微妙。考虑第三段的开头,其中论证了如果爱丽丝的丈夫在酒吧,那么爱丽丝或她的兄弟就是受害者。抽象出一些细节,该论点具有以下形式:
The rule for deriving an “if … then” statement is more subtle. Consider the beginning of the third paragraph, which argues that if Alice’s husband was in the bar, then Alice or her brother was the victim. Abstracting away some of the details, the argument has the following form:
假设爱丽丝的丈夫在酒吧。
Suppose Alice’s husband was in the bar.
然后 …
Then …
然后 …
Then …
那么爱丽丝或她的兄弟就是受害者。
Then Alice or her brother was the victim.
因此,如果爱丽丝的丈夫在酒吧,那么爱丽丝或她的兄弟就是受害者。
Thus, if Alice’s husband was in the bar, then Alice or her brother was the victim.
这是一种假设推理。假设
This is a form of hypothetical reasoning. On the supposition that
假设被贴上标签
The hypothesis is given the label
2.3.2.连词¶
2.3.2. Conjunction¶
和蕴涵一样,其他逻辑联结词通常以引入和消除规则为特征。引入规则显示如何建立涉及联结词的断言,而消除规则显示如何使用包含联结词的断言导出其他断言。
As was the case for implication, other logical connectives are generally characterized by their introduction and elimination rules. An introduction rule shows how to establish a claim involving the connective, while an elimination rule shows how to use such a statement that contains the connective to derive others.
例如,让我们考虑一下连词的情况,即单词“and”。非正式地,我们通过建立每个连词来建立连词。例如,非正式地,我们可以论证:
Let us consider, for example, the case of conjunction, that is, the word “and.” Informally, we establish a conjunction by establishing each conjunct. For example, informally we might argue:
受害者是爱丽丝的哥哥。
Alice’s brother was the victim.
爱丽丝的丈夫就是凶手。
Alice’s husband was the killer.
因此,爱丽丝的兄弟是受害者,而爱丽丝的丈夫是凶手。
Therefore Alice’s brother was the victim and Alice’s husband was the killer.
这个推论似乎太明显了,无法明确说明,因为“和”这个词只是将两个断言合并为一个。非正式证明通常会淡化这种区别。在符号逻辑中,规则如下:
The inference seems almost too obvious to state explicitly, since the word “and” simply combines the two assertions into one. Informal proofs often downplay the distinction. In symbolic logic, the rule reads as follows:
通过两个消除规则,我们可以提取两个组成部分:
The two elimination rules allow us to extract the two components:
爱丽丝的丈夫在酒吧,而爱丽丝则在海滩上。
Alice’s husband was in the bar and Alice was on the beach.
原来爱丽丝的丈夫在酒吧里。
So Alice’s husband was in the bar.
或者:
Or:
爱丽丝的丈夫在酒吧,而爱丽丝则在海滩上。
Alice’s husband was in the bar and Alice was on the beach.
所以爱丽丝就在海滩上。
So Alice was on the beach.
在符号中,这些模式呈现如下:
In symbols, these patterns are rendered as follows:
这里
Here the
2.3.3.否定与假¶
2.3.3. Negation and Falsity¶
从逻辑上讲,证明“非 A”就等于证明 A 会导致矛盾。例如:
In logical terms, showing “not A” amounts to showing that A leads to a contradiction. For example:
假设爱丽丝的丈夫在酒吧。
Suppose Alice’s husband was in the bar.
…
…
这种情况是不可能的。
This situation is impossible.
因此爱丽丝的丈夫不在酒吧。
Therefore Alice’s husband was not in the bar.
这是另一种假设推理形式,类似于建立“如果……那么”语句时使用的推理:我们暂时假设 A,表明这会导致矛盾,并得出结论“非 A”成立。用符号表示,规则如下:
This is another form of hypothetical reasoning, similar to that used in establishing an “if … then” statement: we temporarily assume A, show that leads to a contradiction, and conclude that “not A” holds. In symbols, the rule reads as follows:
消除规则与这些规则是对偶的。它表示如果我们同时拥有“A”和“非 A”,那么我们就有矛盾。这种模式在下面的非正式论证中得到了说明,它隐含在“Malice and Alice”解决方案的第四段中。
The elimination rule is dual to these. It expresses that if we have both “A” and “not A,” then we have a contradiction. This pattern is illustrated in the informal argument below, which is implicit in the fourth paragraph of the solution to “Malice and Alice.”
凶手是艾丽丝的丈夫,受害者是他怀里的孩子。
The killer was Alice’s husband and the victim was the child he was with.
因此凶手并不比受害者年轻。
So the killer was not younger than his victim.
但根据(6),凶手比受害者年轻。
But according to (6), the killer was younger than his victim.
这种情况是不可能的。
This situation is impossible.
在符号逻辑中,推理规则表示如下:
In symbolic logic, the rule of inference is expressed as follows:
还要注意,在符号框架中,我们引入了一个新符号,
Notice also that in the symbolic framework, we have introduced a new symbol,
有哪些规则
What are the rules governing
消除规则也有一个花哨的拉丁名,ex falso sequitur quodlibet,意思是“任何你想要的结果都源于虚假”。
The elimination rule also has the fancy Latin name, ex falso sequitur quodlibet, which means “anything you want follows from falsity.”
从自然语言的角度来看,这种消除规则很难激发,但尽管如此,它对于捕捉常见的推理模式还是必要的。理解它的一种方法是这样的。考虑以下语句:
This elimination rule is harder to motivate from a natural language perspective, but, nonetheless, it is needed to capture common patterns of inference. One way to understand it is this. Consider the following statement:
对于每个自然数
For every natural number
我们想说这是一个正确的陈述。但如果它是真的,那么对于任何特定的数字来说,它都是正确的
We would like to say that this is a true statement. But if it is true, then it is true of any particular number
如果 2 为质数且大于 2,则 2 为奇数。
If 2 is prime and greater than 2, then 2 is odd.
在这个条件语句中,前件和后件都是假的。我们承诺说这个语句是真的,这一事实表明我们应该能够以某种方式证明,语句 2 是奇数,这是由错误语句 2 是素数且大于 2 得出的。前件假也巧妙地概括了这种推理。
In this conditional statement, both the antecedent and succedent are false. The fact that we are committed to saying that this statement is true shows that we should be able to prove, one way or another, that the statement 2 is odd follows from the false statement that 2 is prime and greater than 2. The ex falso neatly encapsulates this sort of inference.
请注意,如果我们定义
Notice that if we define
引入了表示“假”的符号后,再引入表示“真”的符号才是公平的。与“假”相反,“真”没有消除规则,只有引入规则:
Having introduced a symbol for “false,” it is only fair to introduce a symbol for “true.” In contrast to “false,” “true” has no elimination rule, only an introduction rule:
简单来说,“真”就是真。
Put simply, “true” is true.
2.3.4.析取¶
2.3.4. Disjunction¶
析取(也称为“或”)的引入规则很简单。例如,在提出的解决方案中,条件 (3) 得到满足的说法可以证明如下:
The introduction rules for disjunction, otherwise known as “or,” are straightforward. For example, the claim that condition (3) is met in the proposed solution can be justified as follows:
谋杀案发生时,爱丽丝的女儿独自一人。
Alice’s daughter was alone at the time of the murder.
因此,要么爱丽丝的女儿在谋杀案发生时独自一人,要么爱丽丝的儿子在谋杀案发生时独自一人。
Therefore, either Alice’s daughter was alone at the time of the murder, or Alice’s son was alone at the time of the murder.
从象征意义上来说,这两条引入规则如下:
In symbolic terms, the two introduction rules are as follows:
在这里,再次
Here, again, the
析取消元法规则比较复杂,但它代表了一种基于案例的假设推理的自然形式。在“恶意和爱丽丝”的解决方案中出现的情况都是此规则的特殊情况,因此,构造一个新的例子来说明普遍现象将很有帮助。假设,在上面的论证中,我们已经确定爱丽丝的兄弟或儿子在酒吧,并且我们想为她的丈夫在海滩上的结论辩护。一种选择是通过案例进行辩论:首先,考虑她兄弟在酒吧的情况,并根据该假设为结论辩护;然后考虑她儿子在酒吧的情况,并根据第二个假设为同一结论辩护。由于这两个案例是详尽无遗的,如果我们知道结论在每种情况下都成立,我们就知道它完全成立。模式看起来像这样:
The disjunction elimination rule is trickier, but it represents a natural form of case-based hypothetical reasoning. The instances that occur in the solution to “Malice and Alice” are all special cases of this rule, so it will be helpful to make up a new example to illustrate the general phenomenon. Suppose, in the argument above, we had established that either Alice’s brother or her son was in the bar, and we wanted to argue for the conclusion that her husband was on the beach. One option is to argue by cases: first, consider the case that her brother was in the bar, and argue for the conclusion on the basis of that assumption; then consider the case that her son was in the bar, and argue for the same conclusion, this time on the basis of the second assumption. Since the two cases are exhaustive, if we know that the conclusion holds in each case, we know that it holds outright. The pattern looks something like this:
要么是爱丽丝的兄弟在酒吧里,要么是爱丽丝的儿子在酒吧里。
Either Alice’s brother was in the bar, or Alice’s son was in the bar.
假设,在第一种情况下,她的兄弟在酒吧。那么... 因此,她的丈夫在海滩上。
Suppose, in the first case, that her brother was in the bar. Then … Therefore, her husband was on the beach.
另一方面,假设她的儿子在酒吧。在这种情况下,…因此,在这种情况下,她的丈夫也在海滩上。
On the other hand, suppose her son was in the bar. In that case, … Therefore, in this case also, her husband was on the beach.
无论如何,我们已经确定她的丈夫当时在海滩上。
Either way, we have established that her husband was on the beach.
在符号中,此模式表达如下:
In symbols, this pattern is expressed as follows:
这种模式令人困惑的地方在于,它需要两个嵌套的假设推理实例:在第一个括号中,我们暂时假设
What makes this pattern confusing is that it requires two instances of nested hypothetical reasoning: in the first block of parentheses, we temporarily assume
还有另一种通常与“或”一起使用的推理模式,如下例所示:
There is another pattern of reasoning that is commonly used with “or,” as in the following example:
要么是爱丽丝的丈夫在酒吧,要么是爱丽丝在酒吧。
Either Alice’s husband was in the bar, or Alice was in the bar.
爱丽丝的丈夫不在酒吧。
Alice’s husband was not in the bar.
所以爱丽丝在酒吧里。
So Alice was in the bar.
用符号表示的话,我们将此规则表达如下:
In symbols, we would render this rule as follows:
下一章我们将看到,可以从其他规则中推导出这条规则。因此,我们不会将其作为我们系统中的基本推理规则。
We will see in the next chapter that it is possible to derive this rule from the others. As a result, we will not take this to be a fundamental rule of inference in our system.
2.3.5.当且仅当¶
2.3.5. If and only if¶
在数学论证中,通常会说两个陈述,
In mathematical arguments, it is common to say of two statements,
“恶意与爱丽丝”的条件意味着爱丽丝在酒吧当且仅当爱丽丝的丈夫在海滩上。通过依次论证每个蕴涵,可以建立这样的陈述:
The conditions of “Malice and Alice” imply that Alice is in the bar if and only if Alice’s husband is on the beach. Such a statement is established by arguing for each implication in turn:
我声称爱丽丝在酒吧当且仅当爱丽丝的丈夫在海滩上。
I claim that Alice is in the bar if and only if Alice’s husband is on the beach.
为了理解这一点,首先假设爱丽丝在酒吧里。
To see this, first suppose that Alice is in the bar.
然后 …
Then …
因此爱丽丝的丈夫就在海滩上。
Hence Alice’s husband is on the beach.
相反,假设爱丽丝的丈夫在海滩上。
Conversely, suppose Alice’s husband is on the beach.
然后 …
Then …
因此爱丽丝就在酒吧里。
Hence Alice is in the bar.
请注意,在这个例子中,我们改变了陈述的形式,首先陈述结论,而不是在论证结束时陈述。这种“指示”在非正式论证中很常见,因为它有助于引导读者的期望并预示论证的发展方向。正式的演绎系统通常不会模拟这种细微差别,这一事实标志着正式论证和非正式论证之间的差异,我们将在下文中讨论这个话题。
Notice that with this example, we have varied the form of presentation, stating the conclusion first, rather than at the end of the argument. This kind of “signposting” is common in informal arguments, in that is helps guide the reader’s expectations and foreshadow where the argument is going. The fact that formal systems of deduction do not generally model such nuances marks a difference between formal and informal arguments, a topic we will return to below.
引言以自然演绎建模如下:
The introduction is modeled in natural deduction as follows:
iff 的消元规则并不复杂。用非正式的语言来说,这是“左”规则:
The elimination rules for iff are unexciting. In informal language, here is the “left” rule:
当且仅当爱丽丝的丈夫在海滩上时,爱丽丝才会在酒吧里。
Alice is in the bar if and only if Alice’s husband is on the beach.
爱丽丝在酒吧里。
Alice is in the bar.
因此,爱丽丝的丈夫就在海滩上。
Hence, Alice’s husband is on the beach.
而“正确”的规则恰恰相反。
The “right” rule simply runs in the opposite direction.
当且仅当爱丽丝的丈夫在海滩上时,爱丽丝才会在酒吧里。
Alice is in the bar if and only if Alice’s husband is on the beach.
爱丽丝的丈夫在海滩上。
Alice’s husband is on the beach.
因此,爱丽丝在酒吧里。
Hence, Alice is in the bar.
根据自然演绎推理,规则如下:
Rendered in natural deduction, the rules are as follows:
2.3.6.矛盾证明¶
2.3.6. Proof by Contradiction¶
我们看到了一个非正式论证的例子,它隐式地使用了否定的引入规则:
We saw an example of an informal argument that implicitly uses the introduction rule for negation:
假设爱丽丝的丈夫在酒吧。
Suppose Alice’s husband was in the bar.
…
…
这种情况是不可能的。
This situation is impossible.
因此爱丽丝的丈夫不在酒吧。
Therefore Alice’s husband was not in the bar.
考虑以下论点:
Consider the following argument:
假设爱丽丝的丈夫不在海滩上。
Suppose Alice’s husband was not on the beach.
…
…
这种情况是不可能的。
This situation is impossible.
因此爱丽丝的丈夫就在海滩上。
Therefore Alice’s husband was on the beach.
乍一看,你可能会认为这个论证遵循与前一个论证相同的模式。但仔细观察就会发现一个区别:在第一个论证中,结论中引入了一个否定,而在第二个论证中,否定从假设中被消除。使用否定引入来结束第二个论证将得出结论“爱丽丝的丈夫不在海滩上的情况并非如此。”用肯定的陈述“爱丽丝的丈夫在海滩上”取代结论的推理规则称为矛盾证明。(它还有一个花哨的名字,即“归谬法”,即“归谬法”)。
At first glance, you might think this argument follows the same pattern as the one before. But a closer look should reveal a difference: in the first argument, a negation is introduced into the conclusion, whereas in the second, it is eliminated from the hypothesis. Using negation introduction to close the second argument would yield the conclusion “It is not the case that Alice’s husband was not on the beach.” The rule of inference that replaces the conclusion with the positive statement that Alice’s husband was on the beach is called a proof by contradiction. (It also has a fancy name, reductio ad absurdum, “reduction to an absurdity.”)
可能很难看出这两条规则之间的区别,因为我们通常认为“爱丽丝的丈夫不在海滩上”这句话是爱丽丝的丈夫在海滩上的拐弯抹角和近乎不合语法的说法。事实上,这条规则相当于增加了一条公理,即对于每个语句 A,“不不是 A”等同于 A。
It may be hard to see the difference between the two rules, because we commonly take the statement “Alice’s husband was not not on the beach” to be a roundabout and borderline ungrammatical way of saying that Alice’s husband was on the beach. Indeed, the rule is equivalent to adding an axiom that says that for every statement A, “not not A” is equivalent to A.
有一种数学方法被称为“构造性数学”,它否定“非非 A”与 A 的等价性。构造性论证往往具有更好的计算解释;某事为真的证明应该提供明确的证据证明该陈述为真,而不是提供该陈述不可能为假的证据。我们将在后面的章节中讨论构造性推理。尽管如此,矛盾证明在当代数学中被广泛使用,因此,在此期间,我们将自由使用矛盾证明作为我们的基本规则之一。
There is a style of doing mathematics known as “constructive mathematics” that denies the equivalence of “not not A” and A. Constructive arguments tend to have much better computational interpretations; a proof that something is true should provide explicit evidence that the statement is true, rather than evidence that it can’t possibly be false. We will discuss constructive reasoning in a later chapter. Nonetheless, proof by contradiction is used extensively in contemporary mathematics, and so, in the meanwhile, we will use proof by contradiction freely as one of our basic rules.
在自然演绎中,矛盾证明用以下模式表达:
In natural deduction, proof by contradiction is expressed by the following pattern:
假设
The assumption
2.4.命题逻辑的语言¶
2.4. The Language of Propositional Logic¶
命题逻辑的语言始于符号
The language of propositional logic starts with symbols
是命题公式的一个例子。
is an example of a propositional formula.
在用符号逻辑编写表达式时,我们将采用一种运算顺序,这样我们就可以删除多余的括号。解析表达式时:
When writing expressions in symbolic logic, we will adopt an order of operations which allows us to drop superfluous parentheses. When parsing an expression:
否定的结合最为紧密。
Negation binds most tightly.
然后,连词和析取词从右到左绑定。
Then, conjunctions and disjunctions bind from right to left.
最后,蕴涵和双重蕴涵从右到左绑定。
Finally, implications and bi-implications bind from right to left.
例如,表达式
So, for example, the expression
例如,假设我们分配以下变量:
For example, suppose we assign the following variables:
:爱丽丝的丈夫在酒吧里 : Alice’s husband was in the bar :爱丽丝在海滩上 : Alice was on the beach :爱丽丝在酒吧里 : Alice was in the bar :爱丽丝的丈夫在海滩上 : Alice’s husband was on the beach
那么“要么爱丽丝的丈夫在酒吧,爱丽丝在海滩上,或者爱丽丝在酒吧,爱丽丝的丈夫在海滩上”这句话将被表达为
Then the statement “either Alice’s husband was in the bar and Alice was on the beach, or Alice was in the bar and Alice’s husband was on the beach” would be rendered as
然而,有时合适的翻译并不那么简单。由于自然语言更加灵活和细致入微,因此需要一定程度的抽象和规范来进行翻译。有时不同的翻译可以说是合理的。在令人满意的情况下,替代翻译在逻辑上是等价的,即人们可以使用纯逻辑规则从另一个翻译中得出一个翻译。在不太令人满意的情况下,翻译将不等价,在这种情况下,从逻辑的角度来看,原始陈述只是模棱两可的。在这种情况下,选择符号表示有助于澄清预期的含义。
Sometimes the appropriate translation is not so straightforward, however. Because natural language is more flexible and nuanced, a degree of abstraction and regimentation is needed to carry out the translation. Sometimes different translations are arguably reasonable. In happy situations, alternative translations will be logically equivalent, in the sense that one can derive each from the other using purely logical rules. In less happy situations, the translations will not be equivalent, in which case the original statement is simply ambiguous, from a logical point of view. In cases like that, choosing a symbolic representation helps clarify the intended meaning.
例如,考虑这样一句话:“爱丽丝和她的儿子在海滩上,但她的丈夫独自一人。”我们可以选择以下变量:
Consider, for example, a statement like “Alice was with her son on the beach, but her husband was alone.” We might choose variables as follows:
:爱丽丝在海滩上 : Alice was on the beach :爱丽丝的儿子在海滩上 : Alice’s son was on the beach :爱丽丝的丈夫独自一人 : Alice’s husband was alone
在这种情况下,我们可以用符号表示语句为
In that case, we might represent the statement in symbols as
2.5.练习¶
2.5. Exercises¶
这是乔治·J·萨默斯 (George J. Summers) 的另一个(可怕的)逻辑谜题,名为“家庭谋杀案”。
一天晚上,一对父母和他们的儿子和女儿的家里发生了一起谋杀案。一名家庭成员谋杀了另一名家庭成员,第三名家庭成员目击了整个过程,第四名家庭成员则是事后共犯。
同案犯与证人性别相反。
最年长的成员与证人是异性。
最年轻的成员与受害者是异性。
该附属品的年龄比受害者的年龄还要大。
父亲是成员中年龄最大的。
凶手并不是最年轻的成员。
父亲、母亲、儿子、女儿这四个人中,哪一个是凶手?
解决这个难题,并写出一个清晰的论据来证明你的答案是正确的。
Here is another (gruesome) logic puzzle by George J. Summers, called “Murder in the Family.”
Murder occurred one evening in the home of a father and mother and their son and daughter. One member of the family murdered another member, the third member witnessed the crime, and the fourth member was an accessory after the fact.
The accessory and the witness were of opposite sex.
The oldest member and the witness were of opposite sex.
The youngest member and the victim were of opposite sex.
The accessory was older than the victim.
The father was the oldest member.
The murderer was not the youngest member.
Which of the four—father, mother, son, or daughter—was the murderer?
Solve this puzzle, and write a clear argument to establish that your answer is correct.
使用助记符
(父亲), (母亲), (女儿), (儿子), (凶手), (受害者), (证人), (配饰), (最老的), (最年轻),我们可以定义命题变量,如 (父亲是凶手) (女儿是受害者) (父亲是长子), (受害者是最年轻的)等等。请注意,只有儿子或女儿可以是最小的,只有母亲或父亲可以是最年长的。按照这些约定,第一个线索可以表示为
换句话说,如果父亲或儿子是从犯,那么母亲或女儿就是证人,反之亦然。以类似的方式表示其他五条线索。
表示第四条线索比较棘手。尝试写下一个公式,描述信息未排除的所有可能性。
Using the mnemonic
(Father), (Mother), (Daughter), (Son), (Murderer), (Victim), (Witness), (Accessory), (Oldest), (Youngest), we can define propositional variables like (Father is the Murderer), (Daughter is the Victim), (Father is Oldest), (Victim is Youngest), etc. Notice that only the son or daughter can be the youngest, and only the mother or father can be the oldest.With these conventions, the first clue can be represented as
in other words, if the father or son was the accessory, then the mother or daughter was the witness, and vice-versa. Represent the other five clues in a similar manner.
Representing the fourth clue is tricky. Try to write down a formula that describes all the possibilities that are not ruled out by the information.
考虑以下三个假设:
艾伦喜欢袋鼠,而贝蒂喜欢青蛙,或者卡尔喜欢仓鼠。
如果贝蒂喜欢青蛙,那么艾伦就不喜欢袋鼠。
如果卡尔喜欢仓鼠,那么贝蒂就喜欢青蛙。
写一个清晰的论据来表明这三个假设是矛盾的。
Consider the following three hypotheses:
Alan likes kangaroos, and either Betty likes frogs or Carl likes hamsters.
If Betty likes frogs, then Alan doesn’t like kangaroos.
If Carl likes hamsters, then Betty likes frogs.
Write a clear argument to show that these three hypotheses are contradictory.
3.命题逻辑的自然演绎¶
3. Natural Deduction for Propositional Logic¶
回顾上一章的论证,我们发现,直观地说,有些推论是有效的,有些则不是。例如,如果在推理链中,我们建立了“
Reflecting on the arguments in the previous chapter, we see that, intuitively speaking, some inferences are valid and some are not. For example, if, in a chain of reasoning, we had established “
符号逻辑的任务是开发一种精确的数学理论,解释哪些推论是有效的以及为什么有效。有两种一般方法来阐明有效性的概念。在本章中,我们将考虑演绎方法:如果推论可以通过反映所涉及逻辑术语含义的基本推理规则来证明,则该推论是有效的。在第 6 章中,我们将考虑“语义”方法:如果推论是模式的一个实例,并且该模式始终从真实的假设中得出真实的结论,则该推论是有效的。
The task of symbolic logic is to develop a precise mathematical theory that explains which inferences are valid and why. There are two general approaches to spelling out the notion of validity. In this chapter, we will consider the deductive approach: an inference is valid if it can be justified by fundamental rules of reasoning that reflect the meaning of the logical terms involved. In Chapter 6 we will consider the “semantic” approach: an inference is valid if it is an instance of a pattern that always yields a true conclusion from true hypotheses.
3.1.自然演绎中的推导¶
3.1. Derivations in Natural Deduction¶
我们已经看到,命题逻辑的语言允许我们从命题变量构建表达式
We have seen that the language of propositional logic allows us to build up expressions from propositional variables
在自然演绎中,每个证明都是基于假设的证明。换句话说,在任何证明中,都有一组有限的假设
In natural deduction, every proof is a proof from hypotheses. In other words, in any proof, there is a finite set of hypotheses
和公式一样,证明也是通过按照规则将较小的证明组合在一起而建立的。例如,阅读 and-introduction 规则的方法
Like formulas, proofs are built by putting together smaller proofs, according to the rules. For instance, the way to read the and-introduction rule
如下:如果你有证据
is as follows: if you have a proof
在一些自然演绎的表示中,证明被写成一系列行,其中每一行都可以引用任何前面的行来证明。但在这里我们将采用严格的二维图解格式,其中每个推论的前提都出现在结论的正上方。这使得检查证明并检查其是否正确变得容易:每个推论都应该是用特定公式实例化规则之一中的字母的结果。
In some presentations of natural deduction, a proof is written as a sequence of lines in which each line can refer to any previous lines for justification. But here we will adopt a rigid two-dimensional diagrammatic format in which the premises of each inference appear immediately above the conclusion. This makes it easy to look over a proof and check that it is correct: each inference should be the result of instantiating the letters in one of the rules with particular formulas.
使自然演绎令人困惑的一件事是,当你以这种方式整理证明时,假设可以被消除,或者,正如我们所说的,被取消。例如,我们可以将隐含引入规则应用于最后一个证明,并得到以下证明
One thing that makes natural deduction confusing is that when you put together proofs in this way, hypotheses can be eliminated, or, as we will say, canceled. For example, we can apply the implies-introduction rule to the last proof, and obtain the following proof of
这里,我们使用标签 1 来表示假设
Here, we have used the label 1 to indicate the place where the hypothesis
我们可以继续取消假设
We can continue to cancel the hypothesis
结果是仅使用假设的证明
The result is a proof using only the hypothesis
最终的证明根本不使用任何假设。换句话说,它直接确立了结论。
The resulting proof uses no hypothesis at all. In other words, it establishes the conclusion outright.
请注意,在第二步中,我们取消了假设的两个“副本”
Notice that in the second step, we canceled two “copies” of the hypothesis
在这个证明中,
In this proof, zero copies of
还请注意,尽管我们使用的字母
Also notice that although we are using letters like
最后,还要注意的是,在这些例子中,我们假设了一个特殊规则作为构建证明的起点。它被称为假设规则,它看起来像这样:
Finally, notice also that in these examples, we have assumed a special rule as the starting point for building proofs. It is called the assumption rule, and it looks like this:
这意味着在任何时候我们都可以自由地假设一个公式,
What it means is that at any point we are free to simply assume a formula,
其余的推理规则在上一章中给出,我们在此进行总结。
The remaining rules of inference were given in the last chapter, and we summarize them here.
含义:
Implication:
连词:
Conjunction:
否定:
Negation:
析取:
Disjunction:
真与假:
Truth and falsity:
双重含义:
Bi-implication:
归谬法(反证法):
Reductio ad absurdum (proof by contradiction):
3.2.示例¶
3.2. Examples¶
让我们再考虑一些自然演绎证明的例子。在每种情况下,你都应该思考公式说了什么,以及每一步调用了哪条推理规则。还要密切注意每个阶段取消了哪些假设。如果你查看树的任何一个节点,那么在这一点上已经确定的是,该主张遵循它上面所有尚未取消的假设。
Let us consider some more examples of natural deduction proofs. In each case, you should think about what the formulas say and which rule of inference is invoked at each step. Also pay close attention to which hypotheses are canceled at each stage. If you look at any node of the tree, what has been established at that point is that the claim follows from all the hypotheses above it that haven’t been canceled yet.
以下是证明
The following is a proof of
直观地看,公式
Intuitively, the formula
“内化”了先前证明的结论。
“internalizes” the conclusion of the previous proof. The
下一个证明表明,如果结论
The next proof shows that if a conclusion,
下一个证明的结论可以解释为,如果其中一个不是
The conclusion of the next proof can be interpreted as saying that if it is not the case that one of
最后,接下来的两个例子说明了ex falso规则的用法。第一个是任意公式的推导
Finally, the next two examples illustrate the use of the ex falso rule. The first is a derivation of an arbitrary formula
第二项显示
The second shows that
在一些证明系统中,这些规则被视为系统的一部分。但是在我们的系统中我们不需要这样做:这两个例子表明这些规则可以从我们的其他规则中推导出来。
In some proof systems, these rules are taken to be part of the system. But we do not need to that with our system: these two examples show that the rules can be derived from our other rules.
3.3.正向和反向推理¶
3.3. Forward and Backward Reasoning¶
自然演绎应该代表我们在处理逻辑谜题时使用的推理和论证模式的理想化模型,例如,在上一章中。它们之间存在明显的区别:我们用符号和二维图表描述自然演绎证明,而我们的非正式论证则用文字和段落来写。值得反思一下该模型捕捉到了什么。自然演绎应该阐明我们逻辑论证的形式和结构,描述证明结论的适当方法,并解释我们使用的规则在何种意义上是有效的。
Natural deduction is supposed to represent an idealized model of the patterns of reasoning and argumentation we use, for example, when working with logic puzzles as in the last chapter. There are obvious differences: we describe natural deduction proofs with symbols and two-dimensional diagrams, whereas our informal arguments are written with words and paragraphs. It is worthwhile to reflect on what is captured by the model. Natural deduction is supposed to clarify the form and structure of our logical arguments, describe the appropriate means of justifying a conclusion, and explain the sense in which the rules we use are valid.
构建自然演绎证明可能会令人困惑,但思考它为什么令人困惑是有帮助的。例如,我们可以决定自然演绎不是逻辑推理的好模型。或者我们可能会得出结论,自然演绎的令人困惑的特征告诉我们一些关于普通论证的有趣之处。
Constructing natural deduction proofs can be confusing, but it is helpful to think about why it is confusing. We could, for example, decide that natural deduction is not a good model for logical reasoning. Or we might come to the conclusion that the features of natural deduction that make it confusing tell us something interesting about ordinary arguments.
在“官方”描述中,自然演绎证明是通过将较小的证明放在一起得到较大的证明来构建的。为了证明
In the “official” description, natural deduction proofs are constructed by putting smaller proofs together to obtain bigger ones. To prove
然后我们利用这两个证明构造以下证明:
Then we use these two proofs to construct the following one:
最后,我们将隐含引入规则应用于该证明以取消假设并得到所需的结论:
Finally, we apply the implies-introduction rule to this proof to cancel the hypothesis and obtain the desired conclusion:
该过程与非正式争论中发生的过程类似,我们从一些假设开始,然后逐步得出结论。
The process is similar to what happens in an informal argument, where we start with some hypotheses, and work forward towards a conclusion.
假设苏珊很高并且约翰很高兴。
Suppose Susan is tall and John is happy.
那么,约翰尤其高兴。
Then, in particular, John is happy.
此外,苏珊很高。
Also, Susan is tall.
所以约翰很高兴,苏珊也长高了。
So John is happy and Susan is tall.
因此,我们表明,如果苏珊身高较高且约翰很高兴,那么约翰也会很高兴且苏珊身高较高。
Therefore we have shown that if Susan is tall and John is happy, then John is happy and Susan is tall.
然而,当我们阅读自然演绎证明时,我们经常会倒着读。首先,我们查看底部以查看要证明的内容。然后我们考虑用于证明它的规则,并查看规则要求的前提。然后我们查看如何证明这些主张,等等。同样,当我们构建自然演绎证明时,我们通常也会倒着读:我们从要证明的主张开始,将其放在底部,然后寻找要应用的规则。
However, when we read natural deduction proofs, we often read them backward. First, we look at the bottom to see what is being proved. Then we consider the rule that is used to prove it, and see what premises the rule demands. Then we look to see how those claims are proved, and so on. Similarly, when we construct a natural deduction proof, we typically work backward as well: we start with the claim we are trying to prove, put that at the bottom, and look for rules to apply.
有时这个过程会崩溃。假设我们只剩下一个目标,那就是一个命题变量,
At times that process breaks down. Suppose we are left with a goal that is a single propositional variable,
因此,存在一种用于证明自然演绎定理的一般启发式方法:
There is thus a general heuristic for proving theorems in natural deduction:
首先从结论开始,使用引言规则。例如,如果你试图证明以下形式的陈述
, 添加 你的假设列表,并尝试推导出 . 如果你试图证明以下形式的陈述 ,使用 and-introduction 规则将你的任务简化为证明 ,然后证明 。Start by working backward from the conclusion, using the introduction rules. For example, if you are trying to prove a statement of the form
, add to your list of hypotheses and try to derive . If you are trying to prove a statement of the form , use the and-introduction rule to reduce your task to proving , and then proving .当你在第一步中没有事情要做时,使用消除规则来继续前进。如果你有假设
和 ,运用肯定前件推导出 . 如果你有一个假设 ,使用或消除法来拆分案例,考虑 在一个案例中 在另一个。When you have run out things to do in the first step, use elimination rules to work forward. If you have hypotheses
and , apply modus ponens to derive . If you have a hypothesis , use or-elimination to split on cases, considering in one case and in the other.
在第 5 章中,我们将向此列表添加另一个元素:如果其他方法都失败了,请尝试反证法。
In Chapter 5 we will add one more element to this list: if all else fails, try a proof by contradiction.
正向推理和逆向推理之间的矛盾也存在于数学和其他领域的非正式论证中。当我们证明一个定理时,我们通常会使用假设、假设、定义和背景知识进行正向推理。但我们也会牢记目标,这有助于我们理解前进的步骤。
The tension between forward and backward reasoning is found in informal arguments as well, in mathematics and elsewhere. When we prove a theorem, we typically reason forward, using assumptions, hypotheses, definitions, and background knowledge. But we also keep the goal in mind, and that helps us make sense of the forward steps.
当我们转向交互式定理证明时,我们会看到精益具有支持正向和反向推理的机制。这些机制在非正式论证风格和自然演绎模型之间架起了一座桥梁,从而更清楚地展示了正在发生的事情。
When we turn to interactive theorem proving, we will see that Lean has mechanisms to support both forward and backward reasoning. These form a bridge between informal styles of argumentation and the natural deduction model, and thereby provide a clearer picture of what is going on.
自然演绎证明的另一个令人困惑的特征是,每个假设都有一个范围,也就是说,在证明中只有某些点可以使用假设。当然,这也是非正式数学论证的一个特点。假设一个段落以“让
Another confusing feature of natural deduction proofs is that every hypothesis has a scope, which is to say, there are only certain points in the proof where an assumption is available for use. Of course, this is also a feature of informal mathematical arguments. Suppose a paragraph begins “Let
在自然演绎中,假设从被假定的点到被取消的点都是可用的。我们将看到,交互式定理证明语言也有确定参考和假设范围的机制,这些机制也揭示了非形式数学中的范围问题。
In natural deduction, a hypothesis is available from the point where it is assumed until the point where it is canceled. We will see that interactive theorem proving languages also have mechanisms to determine the scope of references and hypotheses, and that these, too, shed light on scoping issues in informal mathematics.
3.4.案例推理¶
3.4. Reasoning by Cases¶
消除析取的规则令人困惑,但我们可以通过一个例子来理解它。考虑以下非正式论证:
The rule for eliminating a disjunction is confusing, but we can make sense of it with an example. Consider the following informal argument:
乔治要么在家,要么在校园。
George is either at home or on campus.
如果他在家,他就在学习。
If he is at home, he is studying.
如果他在校园,他就和他的朋友在一起。
If he is on campus, he is with his friends.
因此,乔治要么在学习,要么和他的朋友在一起。
Therefore, George is either studying or with his friends.
让
Let
乔治要么在家,要么在校园。
Georges is either at home or on campus.
情况 1:假设他在家。我们知道如果他在家,那么他就在学习。所以,在这种情况下,他正在学习。因此,在这种情况下,他要么在学习,要么在和朋友在一起。
案例 2:假设他在校园里。我们知道,如果他在校园里,那么他就和他的朋友在一起。所以,在这种情况下,他和他的朋友在一起。因此,在这种情况下,他要么在学习,要么和他的朋友在一起。
Case 1: Suppose he is at home. We know that if he is at home, then he is studying. So, in this case, he is studying. Therefore, in this case, he is either studying or with his friends.
Case 2: Suppose he is on campus. We know that if he is on campus, then he is with his friends. So, in this case, he is with his friends. Therefore, in this case, he is either studying or with his friends.
无论怎样,乔治要么在学习,要么和他的朋友在一起。
Either way, George is either studying or with his friends.
自然演绎证明如下:
The natural deduction proof looks as follows:
你应该思考这个证明的结构如何反映其上方的非正式的基于案例的论点。
You should think about how the structure of this proof reflects the informal case-based argument above it.
再举一个例子,这是
For another example, here is a proof of
3.5.一些逻辑身份¶
3.5. Some Logical Identities¶
两个命题公式,
Two propositional formulas,
作为参考,以下列表包含一些常用的命题等价关系以及一些值得注意的公式。想想为什么直观上这些公式应该是正确的。
For reference, the following list contains some commonly used propositional equivalences, along with some noteworthy formulas. Think about why, intuitively, these formulas should be true.
交换性
:Commutativity of
:交换性
:Commutativity of
:结合性
:Associativity of
:结合性
Associativity of
分配性
超过 :Distributivity of
over :分配性
超过 :Distributivity of
over : 。 .
所有这些都可以通过使用第 3.1 节中列出的基本规则在自然演绎中得出。但其中一些需要使用归谬法规则或反证法,我们尚未详细讨论过。我们将在第 5 章中讨论此规则的使用以及其他经典逻辑模式。
All of these can be derived in natural deduction using the fundamental rules listed in Section 3.1. But some of them require the use of the reductio ad absurdum rule, or proof by contradiction, which we have not yet discussed in detail. We will discuss the use of this rule, and other patterns of classical logic, in the Chapter 5.
3.6.练习¶
3.6. Exercises¶
在自然演绎中构建证明时,仅使用第 3.1 节中给出的规则列表。
When constructing proofs in natural deduction, use only the list of rules given in Section 3.1.
给出自然演绎证明
来自假设 。Give a natural deduction proof of
from hypothesis .给出自然演绎证明
来自假设 。Give a natural deduction proof of
from hypothesis .给出自然演绎证明
。Give a natural deduction proof of
.给出自然演绎证明
来自假设 和 。Give a natural deduction proof of
from hypotheses and .给出自然演绎证明
。Give a natural deduction proof of
.给出自然演绎证明
。Give a natural deduction proof of
.再看一下上一章的练习 3。使用命题变量
, , 和 分别针对“艾伦喜欢袋鼠”“贝蒂喜欢青蛙”和“卡尔喜欢仓鼠”这三个假设,将这三个假设表达为符号公式,然后通过自然演绎从中得出矛盾。Take another look at Exercise 3 in the last chapter. Using propositional variables
, , and for “Alan likes kangaroos,” “Betty likes frogs” and “Carl likes hamsters,” respectively, express the three hypotheses as symbolic formulas, and then derive a contradiction from them in natural deduction.给出自然演绎证明
。Give a natural deduction proof of
.给出自然演绎证明
Give a natural deduction proof of
给出自然演绎证明
从 .(你不需要使用反证法。)Give a natural deduction proof of
from . (You do not need to use proof by contradiction.)给出自然演绎证明
。Give a natural deduction proof of
.给出自然演绎证明
来自假设 。Give a natural deduction proof of
from hypothesis .给出自然演绎证明
来自假设 . 这与证明 ?Give a natural deduction proof of
from hypothesis . How does this differ from a proof of ?给出自然演绎证明
来自假设 。Give a natural deduction proof of
from hypothesis .给出自然演绎证明
来自假设 和 。Give a natural deduction proof of
from hypotheses and .给出自然演绎证明
。Give a natural deduction proof of
.
4.精益中的命题逻辑¶
4. Propositional Logic in Lean¶
在本章中,您将学习如何使用 Lean 编写证明。我们将从纯机械翻译开始,这将使您能够用 Lean 表示任何自然演绎证明。然而,我们将看到,这种编写证明的风格不是很直观,也不会产生非常易读的证明。它也不能很好地扩展。
In this chapter, you will learn how to write proofs in Lean. We will start with a purely mechanical translation that will enable you to represent any natural deduction proof in Lean. We will see, however, that such a style of writing proofs is not very intuitive, nor does it yield very readable proofs. It also does not scale well.
然后,我们将考虑精益提供的一些支持更具前瞻性的论证风格的机制。由于这些证明看起来更像非正式证明,但可以直接转化为自然推理,它们将帮助我们理解两者之间的关系。
We will then consider some mechanisms that Lean offers that support a more forward-directed style of argumentation. Since these proofs look more like informal proofs but can be directly translated to natural deduction, they will help us understand the relationship between the two.
4.1.命题和证明的表达式¶
4.1. Expressions for Propositions and Proofs¶
从本质上讲,Lean 是一种类型检查器。这意味着我们可以编写表达式并要求系统检查它们是否格式正确,还可以要求系统告诉我们它们表示什么类型的对象。试试这个:
At its core, Lean is what is known as a type checker. This means that we can write expressions and ask the system to check that they are well formed, and also ask the system to tell us what type of object they denote. Try this:
variables A B C : Prop
#check A ∧ ¬ B → C
variables A B C : Prop
#check A ∧ ¬ B → C
在该文本的在线版本中,您可以按“试试看!”按钮将示例复制到编辑器窗口,然后将鼠标悬停在文本上的标记上以阅读消息。
In the online version of this text, you can press the “try it!” button to copy the example to an editor window, and then hover over the markers on the text to read the messages.
在示例中,我们声明了三个遍历命题的变量,并要求 Lean 检查表达式。该命令的输出是,它断言的类型为。在 Lean 中,每个格式正确的表达式都有一个类型。A ∧ ¬ B → C#checkA ∧ ¬ B → C : PropA ∧ ¬ B → CProp
In the example, we declare three variables ranging over propositions, and ask Lean to check the expression A ∧ ¬ B → C. The output of the #check command is A ∧ ¬ B → C : Prop, which asserts that A ∧ ¬ B → C is of type Prop. In Lean, every well-formed expression has a type.
逻辑连接词以 unicode 呈现。下表向您展示了如何在编辑器中输入这些符号,还为纯粹主义者提供了 ascii 等效符号。
The logical connectives are rendered in unicode. The following chart shows you how you can type these symbols in the editor, and also provides ascii equivalents, for the purists among you.
统一码 Unicode |
ASCII Ascii |
精益投入 Lean input |
真的 true |
||
错误的 false |
||
¬ ¬ |
不是 not |
|
∧ ∧ |
/\ /\ |
|
∨ ∨ |
\/ \/ |
|
→ → |
-> -> |
|
↔ ↔ |
<-> <-> |
|
∀ ∀ |
全部 forall |
|
∃ ∃ |
存在 exists |
|
λ λ |
乐趣 fun |
|
≠ ≠ |
~= ~= |
|
到目前为止,我们只讨论了列表中的前七项。稍后我们将讨论量词、lambda 和相等性。尝试输入一些表达式并自行检查。您应该尝试将上例中的一个变量更改为D,或在表达式中插入一个无意义的符号,然后查看 Lean 返回的错误消息。
So far, we have only talked about the first seven items on the list. We will discuss the quantifiers, lambda, and equality later. Try typing some expressions and checking them on your own. You should try changing one of the variables in the example above to D, or inserting a nonsense symbol into the expression, and take a look at the error message that Lean returns.
除了声明变量之外,如果P是任何类型的表达式,我们还可以声明为真的Prop假设:P
In addition to declaring variables, if P is any expression of type Prop, we can declare the hypothesis that P is true:
variables A B : Prop
variable h : A ∧ ¬ B
#check h
variables A B : Prop
variable h : A ∧ ¬ B
#check h
形式上,任何命题都可以看作是一种类型,即该命题的证明类型。假设或前提只是该类型的变量。然后,建立证明就是写下正确类型的表达式。例如,如果 是P类型的任何表达式,则是 类型的表达式,是 类型的表达式。换句话说,如果是 的证明, 是通过应用 和 的左消去规则得到的证明的名称:A ∧ Band.left PAand.right PBPA ∧ Band.left P
Formally, what is going on is that any proposition can be viewed as a type, namely, the type of proofs of that proposition. A hypothesis, or premise, is just a variable of that type. Building proofs is then a matter of writing down expressions of the correct type. For example, if P is any expression of type A ∧ B, then and.left P is an expression of type A, and and.right P is an expression of type B. In other words, if P is a proof of A ∧ B, and and.left P is a name for the proof you get by applying the left elimination rule for and:
类似地,是通过应用正确的消去规则得到的证明。因此,继续上面的例子,我们可以写and.right PB
Similarly, and.right P is the proof of B you get by applying the right elimination rule. So, continuing the example above, we can write
variables A B : Prop
variable h : A ∧ ¬ B
#check and.left h
#check and.right h
variables A B : Prop
variable h : A ∧ ¬ B
#check and.left h
#check and.right h
这两个表达式分别代表这两个证明:
The two expressions represent, respectively, these two proofs:
请注意,在这种表示自然演绎证明的方式中,没有“自由浮动”的假设。每个假设都有一个标签。在精益中,我们通常使用诸如h、h1、h2、 等表达式来标记假设,但您可以使用任何您想要的标识符。
Notice that in this way of representing natural deduction proofs, there are no “free floating” hypotheses. Every hypothesis has a label. In Lean, we will typically use expressions like h, h1, h2, … to label hypotheses, but you can use any identifier you want.
如果h1是 的证明A,h2是 的证明B, 则是 的证明。因此我们可以继续上面的例子:and.intro h1 h2A ∧ B
If h1 is a proof of A and h2 is a proof of B, then and.intro h1 h2 is a proof of A ∧ B. So we can continue the example above:
variables A B : Prop
variable h : A ∧ ¬ B
#check and.intro (and.right h) (and.left h)
variables A B : Prop
variable h : A ∧ ¬ B
#check and.intro (and.right h) (and.left h)
这对应于以下证明:
This corresponds to the following proof:
那么蕴涵呢?消去规则很简单:如果P₁是的证明,并且是的证明,那么就是的证明。请注意,我们甚至不需要命名规则:只需写下后跟,就好像您将第一个应用于第二个一样。如果和是复合表达式,请在它们周围加上括号,以明确每个表达式的开始和结束位置。A → BP₂AP₁ P₂BP₁P₂P₁P₂
What about implication? The elimination rule is easy: if P₁ is a proof of A → B and P₂ is a proof of A then P₁ P₂ is a proof of B. Notice that we do not even need to name the rule: you just write P₁ followed by P₂, as though you are applying the first to the second. If P₁ and P₂ are compound expressions, put parentheses around them to make it clear where each one begins and ends.
variables A B C D : Prop
variable h1 : A → (B → C)
variable h2 : D → A
variable h3 : D
variable h4 : B
#check h2 h3
#check h1 (h2 h3)
#check (h1 (h2 h3)) h4
variables A B C D : Prop
variable h1 : A → (B → C)
variable h2 : D → A
variable h3 : D
variable h4 : B
#check h2 h3
#check h1 (h2 h3)
#check (h1 (h2 h3)) h4
Lean 采用了应用程序与左关联的惯例,因此表达式被解释为。蕴涵与右关联,因此被解释为。这看起来可能很有趣,但它是一种表示采用多个假设的蕴涵的方便方法,因为表达式意味着从、、和得出。因此,上面的例子可以写成如下形式:h1 h2 h3(h1 h2) h3A → B → CA → (B → C)A → B → C → D → EEABCD
Lean adopts the convention that applications associate to the left, so that an expression h1 h2 h3 is interpreted as (h1 h2) h3. Implications associate to the right, so that A → B → C is interpreted as A → (B → C). This may seem funny, but it is a convenient way to represent implications that take multiple hypotheses, since an expression A → B → C → D → E means that E follows from A, B, C, and D. So the example above could be written as follows:
variables A B C D : Prop
variable h1 : A → B → C
variable h2 : D → A
variable h3 : D
variable h4 : B
#check h2 h3
#check h1 (h2 h3)
#check h1 (h2 h3) h4
variables A B C D : Prop
variable h1 : A → B → C
variable h2 : D → A
variable h3 : D
variable h4 : B
#check h2 h3
#check h1 (h2 h3)
#check h1 (h2 h3) h4
请注意,表达式中仍然需要括号。h1 (h2 h3)
Notice that parentheses are still needed in the expression h1 (h2 h3).
蕴涵引入规则比较棘手,因为它可以取消假设。就精益表达式而言,该规则翻译如下。假设A和B具有类型Prop,并且假设h是成立的前提A,P是的证明B,可能涉及h。那么表达式就是的证明。例如,我们可以构造如下的证明:assume h : A, PA → BA → A ∧ A
The implication introduction rule is the tricky one, because it can cancel a hypothesis. In terms of Lean expressions, the rule translates as follows. Suppose A and B have type Prop, and, assuming h is the premise that A holds, P is proof of B, possibly involving h. Then the expression assume h : A, P is a proof of A → B. For example, we can construct a proof of A → A ∧ A as follows:
variable A : Prop
#check (assume h : A, and.intro h h)
variable A : Prop
#check (assume h : A, and.intro h h)
请注意,我们不再需要将其声明A为前提。该词assume使前提成为括号中表达式的局部,并且在做出假设后,我们可以引用h。假设是,是的证明,因此表达式是的证明。在这种情况下,我们可以省略括号,因为表达式是明确的:h : Aand.intro h hA ∧ Aassume h : A, and.intro h hA → A ∧ A
Notice that we no longer have to declare A as a premise. The word assume makes the premise local to the expression in parentheses, and after the assumption is made, we can refer to h. Given the assumption h : A, and.intro h h is a proof of A ∧ A, and so the expression assume h : A, and.intro h h is a proof of A → A ∧ A. In this case, we could leave out the parentheses because the expression is unambiguous:
variable A : Prop
#check assume h : A, and.intro h h
variable A : Prop
#check assume h : A, and.intro h h
上面,我们从前提证明了 。我们可以得到如下的证明:¬ B ∧ AA ∧ ¬ BA ∧ ¬ B → ¬ B ∧ A
Above, we proved ¬ B ∧ A from the premise A ∧ ¬ B. We can instead obtain a proof of A ∧ ¬ B → ¬ B ∧ A as follows:
variables A B : Prop
#check (assume h : A ∧ ¬ B, and.intro (and.right h) (and.left h))
variables A B : Prop
#check (assume h : A ∧ ¬ B, and.intro (and.right h) (and.left h))
我们所做的只是将前提转移到本地assume。
All we did was move the premise into a local assume.
(顺便说一下,该assume命令只是 lambda 符号的替代语法,所以我们也可以这样写:
(By the way, the assume command is just alternative syntax for the lambda symbol, so we could also have written this:
variables A B : Prop
#check (λ h : A ∧ ¬ B, and.intro (and.right h) (and.left h))
variables A B : Prop
#check (λ h : A ∧ ¬ B, and.intro (and.right h) (and.left h))
稍后您将了解有关 lambda 符号的更多信息。)
You will learn more about the lambda symbol later.)
4.2.更多命令¶
4.2. More commands¶
让我们引入一个新的 Lean 命令,example。此命令告诉 Lean 您将要证明一个定理,或者更一般地,写下给定类型的表达式。然后应该跟随着证明或表达式本身。
Let us introduce a new Lean command, example. This command tells Lean that you are about to prove a theorem, or, more generally, write down an expression of the given type. It should then be followed by the proof or expression itself.
variables A B : Prop
example : A ∧ ¬ B → ¬ B ∧ A :=
assume h : A ∧ ¬ B,
and.intro (and.right h) (and.left h)
variables A B : Prop
example : A ∧ ¬ B → ¬ B ∧ A :=
assume h : A ∧ ¬ B,
and.intro (and.right h) (and.left h)
当给出此命令时,Lean 会检查后面的表达式:=并确保其类型正确。如果正确,它会接受该表达式作为有效证明。如果不是,它会引发错误。
When given this command, Lean checks the expression after the := and makes sure it has the right type. If so, it accepts the expression as a valid proof. If not, it raises an error.
由于该example命令提供了有关后面表达式(在本例中为被证明的命题)类型的信息,因此有时我们可以省略其他信息。例如,我们可以省略假设的类型:
Because the example command provides information as to the type of the expression that follows (in this case, the proposition being proved), it sometimes enables us to omit other information. For example, we can leave off the type of the assumption:
variables A B : Prop
example : A ∧ ¬ B → ¬ B ∧ A :=
assume h,
and.intro (and.right h) (and.left h)
variables A B : Prop
example : A ∧ ¬ B → ¬ B ∧ A :=
assume h,
and.intro (and.right h) (and.left h)
因为 Lean 知道我们试图用前提来证明一个蕴涵,所以它可以推断出当我们写作时,标识符标记了假设。A ∧ ¬ Bassume hhA ∧ ¬ B
Because Lean knows we are trying to prove an implication with premise A ∧ ¬ B, it can infer that when we write assume h, the identifier h labels the assumption A ∧ ¬ B.
我们也可以反过来,用 这个词为系统提供更多show信息。如果A是一个命题,P是一个证明,那么表达式“ ”与单独的 含义相同,但它表示 是的证明。当 Lean 检查这个表达式时,它会在解析它周围的表达式之前确认确实是 的证明。因此,在我们的例子中,我们也可以这样写:show A, from PPPAPA
We can also go in the other direction, and provide the system with more information, with the word show. If A is a proposition and P is a proof, the expression “show A, from P” means the same thing as P alone, but it signals the intention that P is a proof of A. When Lean checks this expression, it confirms that P really is a proof of A, before parsing the expression surrounding it. So, in our example, we could also write:
variables A B : Prop
example : A ∧ ¬ B → ¬ B ∧ A :=
assume h : A ∧ ¬ B,
show ¬ B ∧ A, from and.intro (and.right h) (and.left h)
variables A B : Prop
example : A ∧ ¬ B → ¬ B ∧ A :=
assume h : A ∧ ¬ B,
show ¬ B ∧ A, from and.intro (and.right h) (and.left h)
我们甚至可以注释较小的表达式和,如下所示:and.right hand.left h
We could even annotate the smaller expressions and.right h and and.left h, as follows:
variables A B : Prop
example : A ∧ ¬ B → ¬ B ∧ A :=
assume h : A ∧ ¬ B,
show ¬ B ∧ A, from and.intro
(show ¬ B, from and.right h)
(show A, from and.left h)
variables A B : Prop
example : A ∧ ¬ B → ¬ B ∧ A :=
assume h : A ∧ ¬ B,
show ¬ B ∧ A, from and.intro
(show ¬ B, from and.right h)
(show A, from and.left h)
这里值得一提的是,Lean 通常会忽略空格,例如缩进和回车。我们本可以将整个示例写在一行中。一般来说,我们将采用缩进和换行的惯例,以显示证明的结构并使其更易于阅读。
This is a good place to mention that Lean generally ignores whitespace, like indentation and returns. We could have written the entire example on a single line. In general, we will adopt conventions for indentation and line breaks that show the structure of proofs and make them easier to read.
尽管在上面的例子中show命令不是必需的,但使用这种风格有很多好的理由。首先,也许最重要的是,它使我们人类更容易阅读证明。其次,它使证明更容易编写:如果你在证明中犯了一个错误,如果你明确表达了意图,Lean 就更容易找出你错在哪里并提供有意义的错误消息。最后,在子句中证明信息show通常使你能够省略其他地方的信息,因为 Lean 可以从你陈述的意图中推断出这些信息。
Although in the examples above the show commands were not necessary, there are a number of good reasons to use this style. First, and perhaps most importantly, it makes the proofs easier for us humans to read. Second, it makes the proofs easier to write: if you make a mistake in a proof, it is easier for Lean to figure out where you went wrong and provide a meaningful error message if you make your intentions clear. Finally, proving information in the show clause often makes it possible for you to omit information in other places, since Lean can infer that information from your stated intentions.
有符号变体。除了事先声明变量和前提之外,您还可以将它们作为示例的“参数”呈现,后跟冒号:
There are notational variants. Rather than declare variables and premises beforehand, you can also present them as “arguments” to the example, followed by a colon:
example (A B : Prop) : A ∧ ¬ B → ¬ B ∧ A :=
assume h : A ∧ ¬ B,
show ¬ B ∧ A, from and.intro (and.right h) (and.left h)
example (A B : Prop) : A ∧ ¬ B → ¬ B ∧ A :=
assume h : A ∧ ¬ B,
show ¬ B ∧ A, from and.intro (and.right h) (and.left h)
还有两个技巧可以帮助你在 Lean 中编写证明。第一个是使用sorry,这是 Lean 中的一个神奇术语,它可以提供任何事物的证明。它也被称为“作弊”。但作弊可以帮助你逐步构建合法的证明:如果 Lean 接受了带有 的证明sorry,那么你迄今为止编写的证明部分就通过了 Lean 的正确性检查。你需要做的就是sorry用真正的证明替换每个证明来完成任务。
There are two more tricks that can help you write proofs in Lean. The first is using sorry, which is a magical term in Lean which provides a proof of anything at all. It is also known as “cheating.” But cheating can help you construct legitimate proofs incrementally: if Lean accepts a proof with sorry’s, the parts of the proof you have written so far have passed Lean’s checks for correctness. All you need to do is replace each sorry with a real proof to complete the task.
variables A B : Prop
example : A ∧ ¬ B → ¬ B ∧ A :=
assume h, sorry
example : A ∧ ¬ B → ¬ B ∧ A :=
assume h, and.intro sorry sorry
example : A ∧ ¬ B → ¬ B ∧ A :=
assume h, and.intro (and.right h) sorry
example : A ∧ ¬ B → ¬ B ∧ A :=
assume h, and.intro (and.right h) (and.left h)
variables A B : Prop
example : A ∧ ¬ B → ¬ B ∧ A :=
assume h, sorry
example : A ∧ ¬ B → ¬ B ∧ A :=
assume h, and.intro sorry sorry
example : A ∧ ¬ B → ¬ B ∧ A :=
assume h, and.intro (and.right h) sorry
example : A ∧ ¬ B → ¬ B ∧ A :=
assume h, and.intro (and.right h) (and.left h)
第二个技巧是使用占位符,用下划线符号表示。当你在表达式中写下下划线时,你是在要求系统尝试为你填写值。这不足以调用全面的自动化来证明定理;相反,你是在要求 Lean 从上下文中推断出值。如果在应该有证明的地方使用下划线,Lean 通常不会填写证明,但会给你一个错误消息,告诉你缺少了什么。这将帮助你以向后驱动的方式逐步编写证明项。在上面的例子中,尝试sorry用下划线替换每个,_然后查看生成的错误消息。在每种情况下,错误都会告诉您需要填写什么,以及该阶段可用的变量和假设。
The second trick is the use of placeholders, represented by the underscore symbol. When you write an underscore in an expression, you are asking the system to try to fill in the value for you. This falls short of calling full-blown automation to prove a theorem; rather, you are asking Lean to infer the value from the context. If you use an underscore where a proof should be, Lean typically will not fill in the proof, but it will give you an error message that tells you what is missing. This will help you write proof terms incrementally, in a backward-driven fashion. In the example above, try replacing each sorry by an underscore, _, and take a look at the resulting error messages. In each case, the error tells you what needs to be filled in, and the variables and hypotheses that are available to you at that stage.
还有一个提示:如果你想要限制命令中引入的变量或前提的范围variables,请将它们放在以单词 开头section并以单词 结尾的块中end。我们将在下面使用这种机制。
One more tip: if you want to delimit the scope of variables or premises introduced with the variables command, put them in a block that begins with the word section and ends with the word end. We will use this mechanism below.
4.3.构建自然演绎证明¶
4.3. Building Natural Deduction Proofs¶
在本节中,我们通过为每个自然演绎规则提供翻译来描述自然演绎证明的机械翻译。我们已经看到了一些对应关系,但为了完整起见,我们在这里重复它们。
In this section, we describe a mechanical translation from natural deduction proofs, by giving a translation for each natural deduction rule. We have already seen some of the correspondences, but we repeat them all here, for completeness.
4.3.1.含义¶
4.3.1. Implication¶
我们已经解释过,蕴涵引入是用 来实现的assume,而蕴涵消除则写为应用。
We have already explained that implication introduction is implemented with assume, and implication elimination is written as application.
variables A B : Prop
example : A → B :=
assume h : A,
show B, from sorry
section
variable h1 : A → B
variable h2 : A
example : B := h1 h2
end
variables A B : Prop
example : A → B :=
assume h : A,
show B, from sorry
section
variable h1 : A → B
variable h2 : A
example : B := h1 h2
end
由于每个例子都是从声明必要的命题变量开始的,因此我们从今以后将在文中删除该声明。
Since every example begins by declaring the necessary propositional variables, we will henceforth suppress that declaration in the text.
4.3.2.连词¶
4.3.2. Conjunction¶
我们已经看到,与引入是用 实现的and.intro,并且消除规则是and.left和and.right。
We have already seen that and-introduction is implemented with and.intro, and the elimination rules are and.left and and.right.
section
variables (h1 : A) (h2 : B)
example : A ∧ B := and.intro h1 h2
end
section
variable h : A ∧ B
example : A := and.left h
example : B := and.right h
end
section
variables (h1 : A) (h2 : B)
example : A ∧ B := and.intro h1 h2
end
section
variable h : A ∧ B
example : A := and.left h
example : B := and.right h
end
4.3.3.析取¶
4.3.3. Disjunction¶
或引入规则由or.inl和给出or.inr。
The or-introduction rules are given by or.inl and or.inr.
section
variable h : A
example : A ∨ B := or.inl h
end
section
variable h : B
example : A ∨ B := or.inr h
end
section
variable h : A
example : A ∨ B := or.inl h
end
section
variable h : B
example : A ∨ B := or.inr h
end
消除规则比较棘手。为了证明C来自,你需要三个参数:来自 的证明、来自的证明和来自的证明。使用换行符和缩进来突出显示结构作为案例证明,我们可以用以下形式编写它:A ∨ BhA ∨ BCACB
The elimination rule is the tricky one. To prove C from A ∨ B, you need three arguments: a proof h of A ∨ B, a proof of C from A, and a proof of C from B. Using line breaks and indentation to highlight the structure as a proof by cases, we can write it with the following form:
section
variable h : A ∨ B
variables (ha : A → C) (hb : B → C)
example : C :=
or.elim h
(assume h1 : A,
show C, from ha h1)
(assume h1 : B,
show C, from hb h1)
end
section
variable h : A ∨ B
variables (ha : A → C) (hb : B → C)
example : C :=
or.elim h
(assume h1 : A,
show C, from ha h1)
(assume h1 : B,
show C, from hb h1)
end
请注意,我们可以在每个分支中重复使用标签h1,因为从概念上讲,这两个分支是不相交的。
Notice that we can reuse the label h1 in each branch, since, conceptually, the two branches are disjoint.
4.3.4.否定¶
4.3.4. Negation¶
在内部,否定定义为,你可以认为这意味着不可能的事情。因此,否定的规则与蕴涵的规则相似。要证明,假设并得出矛盾。¬ AA → falseA¬ AA
Internally, negation ¬ A is defined by A → false, which you can think of as saying that A implies something impossible. The rules for negation are therefore similar to the rules for implication. To prove ¬ A, assume A and derive a contradiction.
section
example : ¬ A :=
assume h : A,
show false, from sorry
end
section
example : ¬ A :=
assume h : A,
show false, from sorry
end
如果你已经证明了一个否定,那么你可以将其应用到的证明中从而得到矛盾。¬ AA
If you have proved a negation ¬ A, you can get a contradiction by applying it to a proof of A.
section
variable h1 : ¬ A
variable h2 : A
example : false := h1 h2
end
section
variable h1 : ¬ A
variable h2 : A
example : false := h1 h2
end
4.3.5.真与假¶
4.3.5. Truth and falsity¶
反面的规则称为false.elim:
The ex falso rule is called false.elim:
section
variable h : false
example : A := false.elim h
end
section
variable h : false
example : A := false.elim h
end
true除了这是显而易见的事实之外,没有什么可说的:
There isn’t much to say about true beyond the fact that it is trivially true:
example : true := trivial
example : true := trivial
4.3.6.双向蕴涵¶
4.3.6. Bi-implication¶
“当且仅当”的引入规则是iff.intro。
The introduction rule for “if and only if” is iff.intro.
example : A ↔ B :=
iff.intro
(assume h : A,
show B, from sorry)
(assume h : B,
show A, from sorry)
example : A ↔ B :=
iff.intro
(assume h : A,
show B, from sorry)
(assume h : B,
show A, from sorry)
和往常一样,我们选择缩进以使结构清晰。请注意,同一个标签h可以在两个分支上使用,每个分支的含义不同,因为 an 的范围assume仅限于它出现的表达式。
As usual, we have chosen indentation to make the structure clear. Notice that the same label, h, can be used on both branches, with a different meaning in each, because the scope of an assume is limited to the expression in which it appears.
消除规则是iff.elim_left和iff.elim_right:
The elimination rules are iff.elim_left and iff.elim_right:
section
variable h1 : A ↔ B
variable h2 : A
example : B := iff.elim_left h1 h2
end
section
variable h1 : A ↔ B
variable h2 : B
example : A := iff.elim_right h1 h2
end
section
variable h1 : A ↔ B
variable h2 : A
example : B := iff.elim_left h1 h2
end
section
variable h1 : A ↔ B
variable h2 : B
example : A := iff.elim_right h1 h2
end
iff.mpLean 识别的缩写iff.elim_left,其中“mp”代表“肯定前件”。同样,您可以使用iff.mpr代替 来表示“肯定前件反向” iff.elim_right。
Lean recognizes the abbreviation iff.mp for iff.elim_left, where “mp” stands for “modus ponens”. Similarly, you can use iff.mpr, for “modus ponens reverse”, instead of iff.elim_right.
4.3.7.归谬法 (反证法证明) ¶
4.3.7. Reductio ad absurdum (proof by contradiction)¶
最后,还有反证法规则,我们将在第 5 章中详细讨论。这里将其纳入其中是为了完整性。
Finally, there is the rule for proof by contradiction, which we will discuss in greater detail in Chapter 5. It is included for completeness here.
该规则称为。它有一个参数,即来自by_contradiction的证明。要使用该规则,您必须要求 Lean 允许经典推理,方法是编写。您可以在文件开头或使用它之前的任何时间执行此操作。如果您在某个部分中说,它将保留在该部分的范围中。false¬ Aopen classicalopen classical
The rule is called by_contradiction. It has one argument, which is a proof of false from ¬ A. To use the rule, you have to ask Lean to allow classical reasoning, by writing open classical. You can do this at the beginning of the file, or any time before using it. If you say open classical in a section, it will remain in scope for that section.
section
open classical
example : A :=
by_contradiction
(assume h : ¬ A,
show false, from sorry)
end
section
open classical
example : A :=
by_contradiction
(assume h : ¬ A,
show false, from sorry)
end
4.3.8.示例¶
4.3.8. Examples¶
在上一章中,我们构造了以下证明
In the last chapter, we constructed the following proof of
我们可以在精益中对此进行如下建模:
We can model this in Lean as follows:
variables A B C : Prop
variable h1 : A → B
variable h2 : B → C
example : A → C :=
assume h : A,
show C, from h2 (h1 h)
variables A B C : Prop
variable h1 : A → B
variable h2 : B → C
example : A → C :=
assume h : A,
show C, from h2 (h1 h)
请注意,自然演绎证明中未被取消的假设在 Lean 版本中被声明为变量。
Notice that the hypotheses in the natural deduction proof that are not canceled are declared as variables in the Lean version.
我们还构造了以下证明:
We also constructed the following proof:
在 Lean 中它的写法如下:
Here is how it is written in Lean:
example (A B C : Prop) : (A → (B → C)) → (A ∧ B → C) :=
assume h1 : A → (B → C),
assume h2 : A ∧ B,
show C, from h1 (and.left h2) (and.right h2)
example (A B C : Prop) : (A → (B → C)) → (A ∧ B → C) :=
assume h1 : A → (B → C),
assume h2 : A ∧ B,
show C, from h1 (and.left h2) (and.right h2)
这是可行的,因为是的证明,并且是的证明。and.left h2Aand.right h2B
This works because and.left h2 is a proof of A, and and.right h2 is a proof of B.
最后,我们构造了以下证明
Finally, we constructed the following proof of
以下是 Lean 的一个版本:
Here is a version in Lean:
example (A B C : Prop) : A ∧ (B ∨ C) → (A ∧ B) ∨ (A ∧ C) :=
assume h1 : A ∧ (B ∨ C),
or.elim (and.right h1)
(assume h2 : B,
show (A ∧ B) ∨ (A ∧ C),
from or.inl (and.intro (and.left h1) h2))
(assume h2 : C,
show (A ∧ B) ∨ (A ∧ C),
from or.inr (and.intro (and.left h1) h2))
example (A B C : Prop) : A ∧ (B ∨ C) → (A ∧ B) ∨ (A ∧ C) :=
assume h1 : A ∧ (B ∨ C),
or.elim (and.right h1)
(assume h2 : B,
show (A ∧ B) ∨ (A ∧ C),
from or.inl (and.intro (and.left h1) h2))
(assume h2 : C,
show (A ∧ B) ∨ (A ∧ C),
from or.inr (and.intro (and.left h1) h2))
事实上,考虑到assume是符号的替代语法λ,并且 Lean 通常可以推断出假设的类型,我们可以使证明非常简短:
In fact, bearing in mind that assume is alternative syntax for the symbol λ, and that Lean can often infer the type of an assumption, we can make the proof remarkably brief:
example (A B C : Prop) : A ∧ (B ∨ C) → (A ∧ B) ∨ (A ∧ C) :=
λ h1, or.elim (and.right h1)
(λ h2, or.inl (and.intro (and.left h1) h2))
(λ h2, or.inr (and.intro (and.left h1) h2))
example (A B C : Prop) : A ∧ (B ∨ C) → (A ∧ B) ∨ (A ∧ C) :=
λ h1, or.elim (and.right h1)
(λ h2, or.inl (and.intro (and.left h1) h2))
(λ h2, or.inr (and.intro (and.left h1) h2))
不过,证明是隐晦的。使用这样的风格使得证明难以编写、阅读、理解、维护和调试。在下一节中,我们将描述一种非常简单的设备,它使人们更容易理解正在发生的事情。
The proof is cryptic, though. Using such a style makes proofs hard to write, read, understand, maintain, and debug. In the next section we will describe a remarkably simple device that makes it much easier to understand what is going on.
4.4.正向推理¶
4.4. Forward Reasoning¶
Lean 允许您使用命令编写证明,从而支持正向推理have。
Lean supports forward reasoning by allowing you to write proofs using the have command.
variables A B C : Prop
variable h1 : A → B
variable h2 : B → C
example : A → C :=
assume h : A,
have h3 : B, from h1 h,
show C, from h2 h3
variables A B C : Prop
variable h1 : A → B
variable h2 : B → C
example : A → C :=
assume h : A,
have h3 : B, from h1 h,
show C, from h2 h3
用 写出证明与写出 的效果相同。此命令检查是 的证明,然后给出 用来代替 的标签。因此,可以将上一个证明的最后一行看作是 的缩写,因为缩写。这种缩写会产生很大的不同,尤其是在证明很长的时候。have h : A, from P, ... h ...... P ...havePAhPshow C, from h2 (h1 h)h3h1 hP
Writing a proof with have h : A, from P, ... h ... has the same effect as writing ... P .... This have command checks that P is a proof of A, and then give you the label h to use in place of P. Thus the last line of the previous proof can be thought of as abbreviating show C, from h2 (h1 h), since h3 abbreviates h1 h. Such abbreviations can make a big difference, especially when the proof P is very long.
使用 有很多好处have。首先,它使证明更具可读性:上面的例子B明确地说明了辅助目标。它还可以节省重复:h3引入后可以重复使用,而无需重复证明。最后,它使构造和调试证明变得更容易:B作为辅助目标陈述使精益更容易在目标未正确实现时提供信息性错误消息。
There are a number of advantages to using have. For one thing, it makes the proof more readable: the example above states B explicitly as an auxiliary goal. It can also save repetition: h3 can be used repeatedly after it is introduced, without duplicating the proof. Finally, it makes it easier to construct and debug the proof: stating B as an auxiliary goal makes it easier for Lean to deliver an informative error message when the goal is not properly met.
在上一节中,我们考虑了以下证明:
In the last section, we considered the following proof:
example (A B C : Prop) : (A → (B → C)) → (A ∧ B → C) :=
assume h1 : A → (B → C),
assume h2 : A ∧ B,
show C, from h1 (and.left h2) (and.right h2)
example (A B C : Prop) : (A → (B → C)) → (A ∧ B → C) :=
assume h1 : A → (B → C),
assume h2 : A ∧ B,
show C, from h1 (and.left h2) (and.right h2)
使用have,可以更清晰地写成如下形式:
Using have, it can be written more perspicuously as follows:
example (A B C : Prop) : (A → (B → C)) → (A ∧ B → C) :=
assume h1 : A → (B → C),
assume h2 : A ∧ B,
have h3 : A, from and.left h2,
have h4 : B, from and.right h2,
show C, from h1 h3 h4
example (A B C : Prop) : (A → (B → C)) → (A ∧ B → C) :=
assume h1 : A → (B → C),
assume h2 : A ∧ B,
have h3 : A, from and.left h2,
have h4 : B, from and.right h2,
show C, from h1 h3 h4
我们还可以更加详细,添加另一行:
We can be even more verbose, and add another line:
example (A B C : Prop) : (A → (B → C)) → (A ∧ B → C) :=
assume h1 : A → (B → C),
assume h2 : A ∧ B,
have h3 : A, from and.left h2,
have h4 : B, from and.right h2,
have h5 : B → C, from h1 h3,
show C, from h5 h4
example (A B C : Prop) : (A → (B → C)) → (A ∧ B → C) :=
assume h1 : A → (B → C),
assume h2 : A ∧ B,
have h3 : A, from and.left h2,
have h4 : B, from and.right h2,
have h5 : B → C, from h1 h3,
show C, from h5 h4
添加更多信息并不总是能使证明更具可读性;当单个表达式很小且足够容易理解时,详细地拼写它们可能会造成混乱。在学习使用精益时,您必须发展自己的风格,并运用您的判断力来决定哪些步骤需要明确。
Adding more information doesn’t always make a proof more readable; when the individual expressions are small and easy enough to understand, spelling them out in detail can introduce clutter. As you learn to use Lean, you will have to develop your own style, and use your judgment to decide which steps to make explicit.
以下是一些基本推论在用 展开后的样子have。在 and-introduction 规则中,首先要显示每个合取项,然后将它们放在一起:
Here is how some of the basic inferences look, when expanded with have. In the and-introduction rule, it is a matter showing each conjunct first, and then putting them together:
example (A B : Prop) : A ∧ B → B ∧ A :=
assume h1 : A ∧ B,
have h2 : A, from and.left h1,
have h3 : B, from and.right h1,
show B ∧ A, from and.intro h3 h2
example (A B : Prop) : A ∧ B → B ∧ A :=
assume h1 : A ∧ B,
have h2 : A, from and.left h1,
have h3 : B, from and.right h1,
show B ∧ A, from and.intro h3 h2
与此版本相比,此版本首先说明我们将使用该and.intro规则,然后明确提出两个最终目标:
Compare that with this version, which instead states first that we will use the and.intro rule, and then makes the two resulting goals explicit:
example (A B : Prop) : A ∧ B → B ∧ A :=
assume h1 : A ∧ B,
show B ∧ A, from
and.intro
(show B, from and.right h1)
(show A, from and.left h1)
example (A B : Prop) : A ∧ B → B ∧ A :=
assume h1 : A ∧ B,
show B ∧ A, from
and.intro
(show B, from and.right h1)
(show A, from and.left h1)
再次强调,问题只在于可读性。Lean 用以下简短版本就做得很好:
Once again, at issue is only readability. Lean does just fine with the following short version:
example (A B : Prop) : A ∧ B → B ∧ A :=
λ h, and.intro (and.right h) (and.left h)
example (A B : Prop) : A ∧ B → B ∧ A :=
λ h, and.intro (and.right h) (and.left h)
使用或消除规则时,最清楚的做法通常是明确说明相关的析取:
When using the or-elimination rule, it is often clearest to state the relevant disjunction explicitly:
example (A B C : Prop) : C :=
have h : A ∨ B, from sorry,
show C, from or.elim h
(assume h1 : A,
show C, from sorry)
(assume h2 : B,
show C, from sorry)
example (A B C : Prop) : C :=
have h : A ∨ B, from sorry,
show C, from or.elim h
(assume h1 : A,
show C, from sorry)
(assume h2 : B,
show C, from sorry)
以下是have上一节中示例的结构化呈现:
Here is a have-structured presentation of an example from the previous section:
example (A B C : Prop) : A ∧ (B ∨ C) → (A ∧ B) ∨ (A ∧ C) :=
assume h1 : A ∧ (B ∨ C),
have h2 : A, from and.left h1,
have h3 : B ∨ C, from and.right h1,
show (A ∧ B) ∨ (A ∧ C), from
or.elim h3
(assume h4 : B,
have h5 : A ∧ B, from and.intro h2 h4,
show (A ∧ B) ∨ (A ∧ C), from or.inl h5)
(assume h4 : C,
have h5 : A ∧ C, from and.intro h2 h4,
show (A ∧ B) ∨ (A ∧ C), from or.inr h5)
example (A B C : Prop) : A ∧ (B ∨ C) → (A ∧ B) ∨ (A ∧ C) :=
assume h1 : A ∧ (B ∨ C),
have h2 : A, from and.left h1,
have h3 : B ∨ C, from and.right h1,
show (A ∧ B) ∨ (A ∧ C), from
or.elim h3
(assume h4 : B,
have h5 : A ∧ B, from and.intro h2 h4,
show (A ∧ B) ∨ (A ∧ C), from or.inl h5)
(assume h4 : C,
have h5 : A ∧ C, from and.intro h2 h4,
show (A ∧ B) ∨ (A ∧ C), from or.inr h5)
4.5.定义和定理¶
4.5. Definitions and Theorems¶
精益允许我们命名定义和定理以供以后使用。例如,这是新“连接词”的定义:
Lean allows us to name definitions and theorems for later use. For example, here is a definition of a new “connective”:
def triple_and (A B C : Prop) : Prop :=
A ∧ (B ∧ C)
def triple_and (A B C : Prop) : Prop :=
A ∧ (B ∧ C)
与命令一样,参数、和是否事先用命令声明,或用定义本身声明example并不重要。然后我们可以将定义应用于任何表达式:ABCvariables
As with the example command, it does not matter whether the arguments A, B, and C are declared beforehand with the variables command, or with the definition itself. We can then apply the definition to any expressions:
variables D E F G : Prop
#check triple_and (D ∨ E) (¬ F → G) (¬ D)
variables D E F G : Prop
#check triple_and (D ∨ E) (¬ F → G) (¬ D)
后面我们还会看到更多有趣的定义的例子,像下面这个从自然数到自然数的函数,它的输入加倍了:
Later, we will see more interesting examples of definitions, like the following function from natural numbers to natural numbers, which doubles its input:
def double (n : ℕ) : ℕ := n + n
def double (n : ℕ) : ℕ := n + n
现在更有趣的是,Lean 还允许我们命名定理,并在以后将其用作推理规则。例如,考虑以下定理:
What is more interesting right now is that Lean also allows us to name theorems, and use them later, as rules of inference. For example, consider the following theorem:
theorem and_commute (A B : Prop) : A ∧ B → B ∧ A :=
assume h, and.intro (and.right h) (and.left h)
theorem and_commute (A B : Prop) : A ∧ B → B ∧ A :=
assume h, and.intro (and.right h) (and.left h)
一旦我们定义了它,我们就可以自由地使用它:
Once we have defined it, we can use it freely:
variables C D E : Prop
variable h1 : C ∧ ¬ D
variable h2 : ¬ D ∧ C → E
example : E := h2 (and_commute C (¬ D) h1)
variables C D E : Prop
variable h1 : C ∧ ¬ D
variable h2 : ¬ D ∧ C → E
example : E := h2 (and_commute C (¬ D) h1)
在这个例子中,我们必须明确给出参数C和,因为它们隐含在中,这很烦人。事实上,Lean 允许我们在 的定义中告诉 Lean 这一点:¬ Dh1and_commute
It is annoying in this example that we have to give the arguments C and ¬ D explicitly, because they are implicit in h1. In fact, Lean allows us to tell this to Lean in the definition of and_commute:
theorem and_commute {A B : Prop} : A ∧ B → B ∧ A :=
assume h, and.intro (and.right h) (and.left h)
theorem and_commute {A B : Prop} : A ∧ B → B ∧ A :=
assume h, and.intro (and.right h) (and.left h)
这里的花括号表示参数A和B是隐式的,也就是说,当使用定理时,Lean 应该从上下文中推断出它们。然后我们可以改写以下内容:
Here the squiggly braces indicate that the arguments A and B are implicit, which is to say, Lean should infer them from the context when the theorem is used. We can then write the following instead:
variables C D E : Prop
variable h1 : C ∧ ¬ D
variable h2 : ¬ D ∧ C → E
example : E := h2 (and_commute h1)
variables C D E : Prop
variable h1 : C ∧ ¬ D
variable h2 : ¬ D ∧ C → E
example : E := h2 (and_commute h1)
事实上,Lean 的库中有一个定理,and_comm其定义正是如此。
Indeed, Lean’s library has a theorem, and_comm, defined in exactly this way.
顺便说一下,我们可以通过将假设变成论点来避免assume证明中的步骤:and_comm
By the way, we could avoid the assume step in the proof of and_comm by making the hypothesis into an argument:
theorem and_commute {A B : Prop} (h : A ∧ B) : B ∧ A :=
and.intro (and.right h) (and.left h)
theorem and_commute {A B : Prop} (h : A ∧ B) : B ∧ A :=
and.intro (and.right h) (and.left h)
这两个定义产生相同的结果。
The two definitions yield the same result.
定义和定理在数学中很重要;它们使我们能够从基本原理构建复杂的理论。精益也接受用这个词lemma来代替theorem。
Definitions and theorems are important in mathematics; they allow us to build up complex theories from fundamental principles. Lean also accepts the word lemma instead of theorem.
有趣的是,在交互式定理证明中,我们甚至可以定义熟悉的推理模式。例如,上一章中提到了以下所有推理:
What is interesting is that in interactive theorem proving, we can even define familiar patterns of inference. For example, all of the following inferences were mentioned in the last chapter:
namespace hidden
variables {A B : Prop}
theorem or_resolve_left (h1 : A ∨ B) (h2 : ¬ A) : B :=
or.elim h1
(assume h3 : A, show B, from false.elim (h2 h3))
(assume h3 : B, show B, from h3)
theorem or_resolve_right (h1 : A ∨ B) (h2 : ¬ B) : A :=
or.elim h1
(assume h3 : A, show A, from h3)
(assume h3 : B, show A, from false.elim (h2 h3))
theorem absurd (h1 : ¬ A) (h2 : A) : B :=
false.elim (h1 h2)
end hidden
namespace hidden
variables {A B : Prop}
theorem or_resolve_left (h1 : A ∨ B) (h2 : ¬ A) : B :=
or.elim h1
(assume h3 : A, show B, from false.elim (h2 h3))
(assume h3 : B, show B, from h3)
theorem or_resolve_right (h1 : A ∨ B) (h2 : ¬ B) : A :=
or.elim h1
(assume h3 : A, show A, from h3)
(assume h3 : B, show A, from false.elim (h2 h3))
theorem absurd (h1 : ¬ A) (h2 : A) : B :=
false.elim (h1 h2)
end hidden
事实上,Lean 的库定义了or.resolve_left、or.resolve_right和absurd。我们使用namespace命令来避免命名冲突,否则会引发错误。
In fact, Lean’s library defines or.resolve_left, or.resolve_right, and absurd. We used the namespace command to avoid naming conflicts, which would have raised an error.
当我们要求您用 Lean 中的命题逻辑证明基本事实时,与命题逻辑一样,我们的目标是让您学会如何使用 Lean 的原语。因此,对于这些练习,您不应该使用库中的事实。然而,随着我们转向真正的数学,您可以更自由地使用库中的事实。
When we ask you to prove basic facts from propositional logic in Lean, as with propositional logic, our goal is to have you learn how to use Lean’s primitives. As a result, for those exercises, you should not use facts from the library. As we move towards real mathematics, however, you can use facts from the library more freely.
4.6.附加语法¶
4.6. Additional Syntax¶
在本节中,我们为高级用户描述了 Lean 的一些额外语法功能。语法小工具通常很方便,有时可以使证明看起来更漂亮。
In this section, we describe some extra syntactic features of Lean, for power users. The syntactic gadgets are often convenient, and sometimes make proofs look prettier.
首先,您可以使用带反斜杠的下标数字。例如,您可以输入h₁。h\1标签与 Lean 无关,因此差异只是表面上的。
For one thing, you can use subscripted numbers with a backslash. For example, you can write h₁ by typing h\1. The labels are irrelevant to Lean, so the difference is only cosmetic.
另一个特点是,你可以在语句中省略标签assume,从而提供“匿名”假设。然后,你可以使用关键字引用最后一个匿名假设this:
Another feature is that you can omit the label in an assume statement, providing an “anonymous” hypothesis. You can then refer back to the last anonymous assumption using the keyword this:
example : A → A ∨ B :=
assume : A,
show A ∨ B, from or.inl this
example : A → A ∨ B :=
assume : A,
show A ∨ B, from or.inl this
或者,您可以通过将未标记的假设放在法语引号中来引用它们:
Alternatively, you can refer back to unlabeled assumptions by putting them in French quotes:
example : A → B → A ∧ B :=
assume : A,
assume : B,
show A ∧ B, from and.intro ‹A› ‹B›
example : A → B → A ∧ B :=
assume : A,
assume : B,
show A ∧ B, from and.intro ‹A› ‹B›
您也可以使用该词have而不给出标签,然后使用相同的惯例引用它们。以下是使用这些功能的示例:
You can also use the word have without giving a label, and refer back to them using the same conventions. Here is an example that uses these features:
theorem my_theorem {A B C : Prop} :
A ∧ (B ∨ C) → (A ∧ B) ∨ (A ∧ C) :=
assume h : A ∧ (B ∨ C),
have A, from and.left h,
have B ∨ C, from and.right h,
show (A ∧ B) ∨ (A ∧ C), from
or.elim ‹B ∨ C›
(assume : B,
have A ∧ B, from and.intro ‹A› ‹B›,
show (A ∧ B) ∨ (A ∧ C), from or.inl this)
(assume : C,
have A ∧ C, from and.intro ‹A› ‹C›,
show (A ∧ B) ∨ (A ∧ C), from or.inr this)
theorem my_theorem {A B C : Prop} :
A ∧ (B ∨ C) → (A ∧ B) ∨ (A ∧ C) :=
assume h : A ∧ (B ∨ C),
have A, from and.left h,
have B ∨ C, from and.right h,
show (A ∧ B) ∨ (A ∧ C), from
or.elim ‹B ∨ C›
(assume : B,
have A ∧ B, from and.intro ‹A› ‹B›,
show (A ∧ B) ∨ (A ∧ C), from or.inl this)
(assume : C,
have A ∧ C, from and.intro ‹A› ‹C›,
show (A ∧ B) ∨ (A ∧ C), from or.inr this)
另一个技巧是,你可以用h.leftandh.right代替and每当是连词,也可以用(使用and ) 代替每当 Lean 能确定你要证明的是连词时。使用这些约定,你可以编写以下内容:and.left hand.right hh⟨h1, h2⟩\<\>and.intro h1 h2
Another trick is that you can write h.left and h.right instead of and.left h and and.right h whenever h is a conjunction, and you can write ⟨h1, h2⟩ (using \< and \>) instead of and.intro h1 h2 whenever Lean can figure out that a conjunction is what you are trying to prove. With these conventions, you can write the following:
example (A B : Prop) : A ∧ B → B ∧ A :=
assume h : A ∧ B,
show B ∧ A, from ⟨h.right, h.left⟩
example (A B : Prop) : A ∧ B → B ∧ A :=
assume h : A ∧ B,
show B ∧ A, from ⟨h.right, h.left⟩
这只不过是下列内容的简写:
This is nothing more than shorthand for the following:
example (A B : Prop) : A ∧ B → B ∧ A :=
assume h : A ∧ B,
show B ∧ A, from and.intro (and.right h) (and.left h)
example (A B : Prop) : A ∧ B → B ∧ A :=
assume h : A ∧ B,
show B ∧ A, from and.intro (and.right h) (and.left h)
更简洁地说,你可以这样写:
Even more concisely, you can write this:
example (A B : Prop) : A ∧ B → B ∧ A :=
assume h, ⟨h.right, h.left⟩
example (A B : Prop) : A ∧ B → B ∧ A :=
assume h, ⟨h.right, h.left⟩
你甚至可以将连词与 分开assume,以便可以这样工作:
You can even take apart a conjunction with an assume, so that this works:
example (A B : Prop) : A ∧ B → B ∧ A :=
assume ⟨h₁, h₂⟩, ⟨h₂, h₁⟩
example (A B : Prop) : A ∧ B → B ∧ A :=
assume ⟨h₁, h₂⟩, ⟨h₂, h₁⟩
类似地,如果h是双条件,则可以用h.mp和h.mpr代替和,也可以用代替。因此,Lean 理解这些证明:iff.mp hiff.mpr h⟨h1, h2⟩iff.intro h1 h2
Similarly, if h is a biconditional, you can write h.mp and h.mpr instead of iff.mp h and iff.mpr h, and you can write ⟨h1, h2⟩ instead of iff.intro h1 h2. As a result, Lean understands these proofs:
example (A B : Prop) : B ∧ (A ↔ B) → A :=
assume ⟨hB, hAB⟩,
hAB.mpr hB
example (A B : Prop) : A ∧ B ↔ B ∧ A :=
⟨assume ⟨h₁, h₂⟩, ⟨h₂, h₁⟩, assume ⟨h₁, h₂⟩, ⟨h₂, h₁⟩⟩
example (A B : Prop) : B ∧ (A ↔ B) → A :=
assume ⟨hB, hAB⟩,
hAB.mpr hB
example (A B : Prop) : A ∧ B ↔ B ∧ A :=
⟨assume ⟨h₁, h₂⟩, ⟨h₂, h₁⟩, assume ⟨h₁, h₂⟩, ⟨h₂, h₁⟩⟩
最后,您可以通过两种方式向证明添加注释。首先,--Lean 处理器会忽略双破折号之后直到行尾的任何文本。其次,/-和之间的任何文本-/表示块注释,也会被忽略。您可以嵌套块注释。
Finally, you can add comments to your proofs in two ways. First, any text after a double-dash -- until the end of a line is ignored by the Lean processor. Second, any text between /- and -/ denotes a block comment, and is also ignored. You can nest block comments.
/- This is a block comment.
It can fill multiple lines. -/
example (A : Prop) : A → A :=
assume : A, -- assume the antecedent
show A, from this -- use it to establish the conclusion
/- This is a block comment.
It can fill multiple lines. -/
example (A : Prop) : A → A :=
assume : A, -- assume the antecedent
show A, from this -- use it to establish the conclusion
4.7.练习¶
4.7. Exercises¶
在精益中证明以下内容:
Prove the following in Lean:
variables A B C D : Prop
example : A ∧ (A → B) → B :=
sorry
example : A → ¬ (¬ A ∧ B) :=
sorry
example : ¬ (A ∧ B) → (A → ¬ B) :=
sorry
example (h₁ : A ∨ B) (h₂ : A → C) (h₃ : B → D) : C ∨ D :=
sorry
example (h : ¬ A ∧ ¬ B) : ¬ (A ∨ B) :=
sorry
example : ¬ (A ↔ ¬ A) :=
sorry
variables A B C D : Prop
example : A ∧ (A → B) → B :=
sorry
example : A → ¬ (¬ A ∧ B) :=
sorry
example : ¬ (A ∧ B) → (A → ¬ B) :=
sorry
example (h₁ : A ∨ B) (h₂ : A → C) (h₃ : B → D) : C ∨ D :=
sorry
example (h : ¬ A ∧ ¬ B) : ¬ (A ∨ B) :=
sorry
example : ¬ (A ↔ ¬ A) :=
sorry
5.经典推理¶
5. Classical Reasoning¶
如果我们采用迄今为止所见的所有命题逻辑规则,并排除归谬法或反证法,我们就得到了所谓的直觉逻辑。在直觉逻辑中,可以从计算的角度来看待证明:
If we take all the rules of propositional logic we have seen so far and exclude reductio ad absurdum, or proof by contradiction, we have what is known as intuitionistic logic. In intuitionistic logic, it is possible to view proofs in computational terms: a proof of
矛盾证明与这种世界观不太相符:从矛盾证明
Proof by contradiction does not fit in well with this world view: from a proof of a contradiction from
然而,经典推理确实在逻辑中引入了许多原则,这些原则可用于简化推理。在本章中,我们将考虑这些原则,并看看它们如何遵循基本规则。
Classical reasoning does introduce a number of principles into logic, however, that can be used to simplify reasoning. In this chapter, we will consider these principles, and see how they follow from the basic rules.
5.1.矛盾证明¶
5.1. Proof by Contradiction¶
请记住,在自然演绎中,矛盾证明用以下模式表达:
Remember that in natural deduction, proof by contradiction is expressed by the following pattern:
假设
The assumption
在精益中,推理被称为by_contradiction,由于它是经典规则,我们必须先使用 命令,它才可用。一旦我们这样做,推理模式表达如下:open classical
In Lean, the inference is named by_contradiction, and since it is a classical rule, we have to use the command open classical before it is available. Once we do so, the pattern of inference is expressed as follows:
open classical
variable (A : Prop)
example : A :=
by_contradiction
(assume h : ¬ A,
show false, from sorry)
open classical
variable (A : Prop)
example : A :=
by_contradiction
(assume h : ¬ A,
show false, from sorry)
该规则的一个最重要的推论就是我们上面提到的经典原理,即排中律,它断言,对于所有em。在数学论证中,人们经常将证明分为两种情况,首先假设
One of the most important consequences of this rule is a classical principle that we mentioned above, namely, the law of the excluded middle, which asserts that the following holds for all em. In mathematical arguments, one often splits a proof into two cases, assuming first
以下是自然演绎中的 的证明em,使用矛盾证明:
Here is a proof of em, in natural deduction, using proof by contradiction:
以下是在 Lean 中呈现的相同证明:
Here is the same proof rendered in Lean:
open classical
variable (A : Prop)
example : A ∨ ¬ A :=
by_contradiction
(assume h1 : ¬ (A ∨ ¬ A),
have h2 : ¬ A, from
assume h3 : A,
have h4 : A ∨ ¬ A, from or.inl h3,
show false, from h1 h4,
have h5 : A ∨ ¬ A, from or.inr h2,
show false, from h1 h5)
open classical
variable (A : Prop)
example : A ∨ ¬ A :=
by_contradiction
(assume h1 : ¬ (A ∨ ¬ A),
have h2 : ¬ A, from
assume h3 : A,
have h4 : A ∨ ¬ A, from or.inl h3,
show false, from h1 h4,
have h5 : A ∨ ¬ A, from or.inr h2,
show false, from h1 h5)
该原理被称为排中律,因为它表明一个命题A要么为真要么为假;没有中间立场。因此,该定理em在 Lean 库中被命名。对于任何命题A,表示 的证明,您可以在任何时候使用它:em AA ∨ ¬ Aclassical
The principle is known as the law of the excluded middle because it says that a proposition A is either true or false; there is no middle ground. As a result, the theorem is named em in the Lean library. For any proposition A, em A denotes a proof of A ∨ ¬ A, and you are free to use it any time classical is open:
open classical
example (A : Prop) : A ∨ ¬ A :=
or.elim (em A)
(assume : A, or.inl this)
(assume : ¬ A, or.inr this)
open classical
example (A : Prop) : A ∨ ¬ A :=
or.elim (em A)
(assume : A, or.inl this)
(assume : ¬ A, or.inr this)
或者更简单一点:
Or even more simply:
open classical
example (A : Prop) : A ∨ ¬ A :=
em A
open classical
example (A : Prop) : A ∨ ¬ A :=
em A
事实上,我们可以反过来,使用排中律来证明反证法。练习中要求你这样做。
In fact, we can go in the other direction, and use the law of the excluded middle to justify proof by contradiction. You are asked to do this in the exercises.
反证法也等同于原理
Proof by contradiction is also equivalent to the principle
以下是 Lean 中的相应证明:
And here is the corresponding proof in Lean:
open classical
example (A : Prop) : ¬ ¬ A ↔ A :=
iff.intro
(assume h1 : ¬ ¬ A,
show A, from by_contradiction
(assume h2 : ¬ A,
show false, from h1 h2))
(assume h1 : A,
show ¬ ¬ A, from assume h2 : ¬ A, h2 h1)
open classical
example (A : Prop) : ¬ ¬ A ↔ A :=
iff.intro
(assume h1 : ¬ ¬ A,
show A, from by_contradiction
(assume h2 : ¬ A,
show false, from h1 h2))
(assume h1 : A,
show ¬ ¬ A, from assume h2 : ¬ A, h2 h1)
在下一节中,我们将推导一些经典规则和等价关系。这些规则和等价关系很难证明。一般来说,要在自然演绎中使用经典推理,我们需要扩展第 3.3 节中提出的一般启发式方法,如下所示:
In the next section, we will derive a number of classical rules and equivalences. These are tricky to prove. In general, to use classical reasoning in natural deduction, we need to extend the general heuristic presented in Section 3.3 as follows:
首先,利用引言规则,从结论开始逆向推导。
First, work backward from the conclusion, using the introduction rules.
当第一步没有事情要做的时候,就用消除规则来继续进行。
When you have run out things to do in the first step, use elimination rules to work forward.
如果其他方法都失败了,就使用反证法。
If all else fails, use a proof by contradiction.
有时反证法是必要的,但当它不是时,它可能比直接证明更不具信息量。例如,假设我们想要证明
Sometimes a proof by contradiction is necessary, but when it isn’t, it can be less informative than a direct proof. Suppose, for example, we want to prove
5.2.一些经典原则¶
5.2. Some Classical Principles¶
我们已经看到
We have already seen that
如果
If
这是另一个例子。直观地说,断言“如果 A 则 B”相当于说 A 为真而 B 为假的情况不可能存在。我们需要经典推理才能从第二个陈述得出第一个陈述。
Here is another example. Intuitively, asserting “if A then B” is equivalent to saying that it cannot be the case that A is true and B is false. Classical reasoning is needed to get us from the second statement to the first.
以下是用 Lean 呈现的相同证明:
Here are the same proofs, rendered in Lean:
open classical
variables (A B : Prop)
example (h : ¬ B → ¬ A) : A → B :=
assume h1 : A,
show B, from
by_contradiction
(assume h2 : ¬ B,
have h3 : ¬ A, from h h2,
show false, from h3 h1)
example (h : ¬ (A ∧ ¬ B)) : A → B :=
assume : A,
show B, from
by_contradiction
(assume : ¬ B,
have A ∧ ¬ B, from and.intro ‹A› this,
show false, from h this)
open classical
variables (A B : Prop)
example (h : ¬ B → ¬ A) : A → B :=
assume h1 : A,
show B, from
by_contradiction
(assume h2 : ¬ B,
have h3 : ¬ A, from h h2,
show false, from h3 h1)
example (h : ¬ (A ∧ ¬ B)) : A → B :=
assume : A,
show B, from
by_contradiction
(assume : ¬ B,
have A ∧ ¬ B, from and.intro ‹A› this,
show false, from h this)
请注意,在第二个例子中,我们使用了匿名assume和匿名have。我们使用括号\f<和\f>来写‹A›,以引用第一个假设。该词的第一次使用this引用了假设,而第二次使用则引用了。¬ Bhave
Notice that in the second example, we used an anonymous assume and an anonymous have. We used the brackets \f< and \f> to write ‹A›, referring back to the first assumption. The first use of the word this refers back to the assumption ¬ B, while the second one refers back to the have.
既然我们可以证明排中律,那么在经典证明中使用它就很方便了。下面是一个例子,其中证明了
Knowing that we can prove the law of the excluded middle, it is convenient to use it in classical proofs. Here is an example, with a proof of
以下是 Lean 中相应的证明:
Here is the corresponding proof in Lean:
open classical
variables (A B : Prop)
example : (A → B) ∨ (B → A) :=
or.elim (em B)
(assume h : B,
have A → B, from
assume : A,
show B, from h,
show (A → B) ∨ (B → A),
from or.inl this)
(assume h : ¬ B,
have B → A, from
assume : B,
have false, from h this,
show A, from false.elim this,
show (A → B) ∨ (B → A),
from or.inr this)
open classical
variables (A B : Prop)
example : (A → B) ∨ (B → A) :=
or.elim (em B)
(assume h : B,
have A → B, from
assume : A,
show B, from h,
show (A → B) ∨ (B → A),
from or.inl this)
(assume h : ¬ B,
have B → A, from
assume : B,
have false, from h this,
show A, from false.elim this,
show (A → B) ∨ (B → A),
from or.inr this)
使用经典推理,蕴涵可以用析取和否定的形式重写:
Using classical reasoning, implication can be rewritten in terms of disjunction and negation:
前进的方向需要经典推理。
The forward direction requires classical reasoning.
以下等价关系被称为德摩根定律:
The following equivalences are known as De Morgan’s laws:
其中第二个问题的前进方向需要经典推理。
The forward direction of the second of these requires classical reasoning.
使用这些恒等式,我们总是可以将否定推到命题变量上。例如,我们有
Using these identities, we can always push negations down to propositional variables. For example, we have
公式建立于
A formula built up from
实际上,利用分配律,可以进一步确保所有析取都在外部,这样公式就是命题变量和否定命题变量的和的大或。这样的公式被称为析取范式。或者,所有和都可以放在外部。这样的公式被称为合取范式。然而,下面的练习表明,将公式置于析取或合取范式中会使它们变得更长。
In fact, using distributivity laws, one can go on to ensure that all the disjunctions are on the outside, so that the formulas is a big or of and’s of propositional variables and negated propositional variables. Such a formula is said to be in disjunctive normal form. Alternatively, all the and’s can be brought to the outside. Such a formula is said to be in conjunctive normal form. An exercise below, however, shows that putting formulas in disjunctive or conjunctive normal form can make them much longer.
5.3.练习¶
5.3. Exercises¶
说明如何利用其他自然演绎规则,从排中律推导出矛盾证明规则。换句话说,假设你有一个证明
从 。 使用 作为假设,但不使用规则 RAA,说明如何继续推导 。Show how to derive the proof-by-contradiction rule from the law of the excluded middle, using the other rules of natural deduction. In other words, assume you have a proof of
from . Using as a hypothesis, but without using the rule RAA, show how you can go on to derive .给出自然演绎证明
从 .(你不需要使用反证法。)Give a natural deduction proof of
from . (You do not need to use proof by contradiction.)构造自然演绎证明
从 。您可以按照如下方式进行操作:首先,证明
,因此 , 从 和 。使用它来构造一个证明
,因此 , 从 和 。用它来构造矛盾的证明
和 。使用矛盾证明,这将为你提供证明
从 。
Construct a natural deduction proof of
from . You can do it as follows:First, prove
, and hence , from and .Use this to construct a proof of
, and hence , from and .Use this to construct a proof of a contradiction from
and .Using proof by contradiction, this gives you a proof of
from .
给出自然演绎证明
从 , , 和 。Give a natural deduction proof of
from , , and .给出自然演绎证明
从 。你可以使用排中律。Give a natural deduction proof of
from . You may use the law of the excluded middle.给出自然演绎证明
。你可以使用排中律。Give a natural deduction proof of
. You may use the law of the excluded middle.放
以析取范式形式,即写成多个“与”表达式的大“或”。Put
in disjunctive normal form, that is, write it as a big “or” of multiple “and” expressions.通过用证明代替下面的抱歉来证明。
¬ (A ∧ B) → ¬ A ∨ ¬ Bopen classical variables {A B C : Prop} -- Prove ¬ (A ∧ B) → ¬ A ∨ ¬ B by replacing the sorry's below -- by proofs. lemma step1 (h₁ : ¬ (A ∧ B)) (h₂ : A) : ¬ A ∨ ¬ B := have ¬ B, from sorry, show ¬ A ∨ ¬ B, from or.inr this lemma step2 (h₁ : ¬ (A ∧ B)) (h₂ : ¬ (¬ A ∨ ¬ B)) : false := have ¬ A, from assume : A, have ¬ A ∨ ¬ B, from step1 h₁ ‹A›, show false, from h₂ this, show false, from sorry theorem step3 (h : ¬ (A ∧ B)) : ¬ A ∨ ¬ B := by_contradiction (assume h' : ¬ (¬ A ∨ ¬ B), show false, from step2 h h')
Prove
¬ (A ∧ B) → ¬ A ∨ ¬ Bby replacing the sorry’s below by proofs.open classical variables {A B C : Prop} -- Prove ¬ (A ∧ B) → ¬ A ∨ ¬ B by replacing the sorry's below -- by proofs. lemma step1 (h₁ : ¬ (A ∧ B)) (h₂ : A) : ¬ A ∨ ¬ B := have ¬ B, from sorry, show ¬ A ∨ ¬ B, from or.inr this lemma step2 (h₁ : ¬ (A ∧ B)) (h₂ : ¬ (¬ A ∨ ¬ B)) : false := have ¬ A, from assume : A, have ¬ A ∨ ¬ B, from step1 h₁ ‹A›, show false, from h₂ this, show false, from sorry theorem step3 (h : ¬ (A ∧ B)) : ¬ A ∨ ¬ B := by_contradiction (assume h' : ¬ (¬ A ∨ ¬ B), show false, from step2 h h')
还可以做这些:
open classical variables {A B C : Prop} example (h : ¬ B → ¬ A) : A → B := sorry example (h : A → B) : ¬ A ∨ B := sorry
Also do these:
open classical variables {A B C : Prop} example (h : ¬ B → ¬ A) : A → B := sorry example (h : A → B) : ¬ A ∨ B := sorry
6.命题逻辑的语义¶
6. Semantics of Propositional Logic¶
传统上,我们认为命题变量涵盖了可能为真或为假的陈述。直观地,我们认为证明系统告诉我们什么命题公式必须为真,无论变量代表什么。例如,我们可以证明
Classically, we think of propositional variables as ranging over statements that can be true or false. And, intuitively, we think of a proof system as telling us what propositional formulas have to be true, no matter what the variables stand for. For example, the fact that we can prove
要理解这一点,就需要跳出系统,对真理做出解释——更准确地说,对命题公式为真的条件做出解释。这是符号逻辑被设计用来做的事情之一,而这项任务属于语义学的范畴。公式和形式证明是句法概念,也就是说,它们由符号和符号结构来表示。真理是一个语义概念,因为它将某种意义赋予某些公式。
Making sense of this involves stepping outside the system and giving an account of truth—more precisely, the conditions under which a propositional formula is true. This is one of the things that symbolic logic was designed to do, and the task belongs to the realm of semantics. Formulas and formal proofs are syntactic notions, which is to say, they are represented by symbols and symbolic structures. Truth is a semantic notion, in that it ascribes a type of meaning to certain formulas.
从句法上来说,我们能够提出和回答如下问题:
Syntactically, we were able to ask and answer questions like the following:
给出一组假设,
,以及公式, ,我们可以得出 从 ?Given a set of hypotheses,
, and a formula, , can we derive from ?可以从哪些公式推导出
?What formulas can be derived from
?需要哪些假设来推导
?What hypotheses are needed to derive
?
我们在语义上考虑的问题是不同的:
The questions we consider semantically are different:
为公式中出现的命题变量赋予真值
, 是 真还是假?Given an assignment of truth values to the propositional variables occurring in a formula
, is true or false?有没有真实的作业
真的?Is there any truth assignment that makes
true?哪些是真实的作业,使得
真的?Which are the truth assignments that make
true?
在本章中,我们不会提供完全严格的语法和语义数学处理。该主题适合更高级、更集中的数理逻辑课程。但我们将详细讨论语义问题,以便让您很好地理解语义思考的含义,以及如何务实地使用语义概念。
In this chapter, we will not provide a fully rigorous mathematical treatment of syntax and semantics. That subject matter is appropriate to a more advanced and focused course on mathematical logic. But we will discuss semantic issues in enough detail to give you a good sense of what it means to think semantically, as well as a sense of how to make pragmatic use of semantic notions.
6.1.真值和赋值¶
6.1. Truth Values and Assignments¶
我们首先需要的概念是真值。我们已经见过两个,即“真”和“假”。我们将使用符号true和false旨在用精益来表示。
The first notion we will need is that of a truth value. We have already seen two, namely, “true” and “false.” We will use the symbols true and false are intended to denote in Lean.
在本文中,我们将采用“经典”的真理概念,就像我们在第 5 节中讨论的那样。这可以用各种方式来理解,但具体来说,可以归结为:我们假设任何命题要么是真,要么是假(当然,不能同时是真和假)。这种真理概念是排中律的基础,
In this text, we will adopt a “classical” notion of truth, following our discussion in Section 5. This can be understood in various ways, but, concretely, it comes down to this: we will assume that any proposition is either true or false (but, of course, not both). This conception of truth is what underlies the law of the excluded middle,
我们需要的下一个概念是真值赋值,它只是一个为命题变量集合的每个元素分配真值的函数。在本节中,我们将使用字母来区分命题变量和任意公式
The next notion we will need is that of a truth assignment, which is simply a function that assigns a truth value to each element of a set of propositional variables. In this section, we will distinguish between propositional variables and arbitrary formulas by using letters
是变量集的真值赋值
is a truth assignment for the set of variables
直观地讲,真值分配描述了一种可能的“世界状态”。回到 Malice 和 Alice 之谜,我们假设以下字母是这些陈述的简写:
Intuitively, a truth assignment describes a possible “state of the world.” Going back to the Malice and Alice puzzle, let’s suppose the following letters are shorthand for the statements:
:= 受害者是爱丽丝的兄弟 := Alice’s brother was the victim := 爱丽丝是凶手 := Alice was the killer := 爱丽丝在酒吧里 := Alice was in the bar
在谜题解描述的世界中,第一和第三条陈述为真,第二条陈述为假。因此我们的真值赋值给出了
In the world described by the solution to the puzzle, the first and third statements are true, and the second is false. So our truth assignment gives the value
一旦我们有了真相任务
Once we have a truth assignment
。 . 。 . , 在哪里 是任何命题变量。 , where is any propositional variable. 如果 是 ,反之亦然。 if is , and vice versa. 如果 和 都是 , 和 否则。 if and are both , and otherwise. 如果至少有一个 和 是 ; 否则 。 if at least one of and is ; otherwise . 如果 是 或者 是 , 和 否则。(等价地, 如果 是 和 是 , 和 否则。) if either is or is , and otherwise. (Equivalently, if is and is , and otherwise.)
合取和析取的规则很容易理解。“
The rules for conjunction and disjunction are easy to understand. “
理解蕴涵规则比较棘手。人们常常惊讶地听到,任何带有错误假设的“如果-那么”语句都应该是正确的。“如果我有两个头,那么圆就是正方形”这句话听起来应该是假的,但根据我们的推算,它是正确的。为了理解这一点,想想这两个句子之间的区别:
Understanding the rule for implication is trickier. People are often surprised to hear that any if-then statement with a false hypothesis is supposed to be true. The statement “if I have two heads, then circles are squares” may sound like it ought to be false, but by our reckoning, it comes out true. To make sense of this, think about the difference between the two sentences:
“如果我有两个头,那么圆形就是方形。”
“If I have two heads, then circles are squares.”
“如果我有两个头,那么圆形就会变成方形。”
“If I had two heads, then circles would be squares.”
第二句是反事实蕴涵的一个例子。它断言如果事情与实际情况不同,世界可能会发生怎样的变化。几个世纪以来,哲学家们一直在研究反事实,但数理逻辑关注的是第一句话,即实质性蕴涵。实质性蕴涵断言的是世界现在的样子,而不是它可能的样子。由于“我有两个头”是错误的,因此“如果我有两个头,那么圆就是正方形”这个说法是正确的。
The second sentence is an example of a counterfactual implication. It asserts something about how the world might change, if things were other than they actually are. Philosophers have studied counterfactuals for centuries, but mathematical logic is concerned with the first sentence, a material implication. The material implication asserts something about the way the world is right now, rather than the way it might have been. Since it is false that I have two heads, the statement “if I have two heads, then circles are squares” is true.
为什么我们要以这种方式评估实质蕴涵?让我们再次考虑这个真实句子“每个大于二的素数自然数都是奇数”。我们可以将这句话解释为,此列表中的所有(无穷多个)句子都是真实的:
Why do we evaluate material implication in this way? Once again, let us consider the true sentence “every natural number that is prime and greater than two is odd.” We can interpret this sentence as saying that all of the (infinitely many) sentences in this list are true:
如果 0 为质数且大于 2,则 0 为奇数。
If 0 is prime and greater than 2, then 0 is odd.
如果 1 为质数且大于 2,则 1 为奇数。
If 1 is prime and greater than 2, then 1 is odd.
如果 2 为质数且大于 2,则 2 为奇数。
If 2 is prime and greater than 2, then 2 is odd.
如果 3 为质数且大于 2,则 3 为奇数。
If 3 is prime and greater than 2, then 3 is odd.
…
…
此列表中的第一句话与我们的“两个头”示例非常相似,因为假设和结论都是错误的。但由于它是一个一般情况下为真的陈述的实例,因此我们致力于赋予它值
The first sentence on this list is a lot like our “two heads” example, since both the hypothesis and the conclusion are false. But since it is an instance of a statement that is true in general, we are committed to assigning it the value
让我们用另一种方式来激发实质蕴涵的语义,使用上一章中描述的演绎规则。请注意,如果
Let us motivate the semantics for material implication another way, using the deductive rules described in the last chapter. Notice that, if
这是从正确解读蕴涵引入规则得出的:给定
This follows from the proper reading of the implication introduction rule: given
variables A B : Prop
variable hB : B
example : A → B :=
assume hA : A,
show B, from hB
variables A B : Prop
variable hB : B
example : A → B :=
assume hA : A,
show B, from hB
类似地,如果
Similarly, if
在精益领域:
In Lean:
variables A B : Prop
variable hnA : ¬ A
example : A → B :=
assume hA : A,
show B, from false.elim (hnA hA)
variables A B : Prop
variable hnA : ¬ A
example : A → B :=
assume hA : A,
show B, from false.elim (hnA hA)
最后,如果
Finally, if
再次强调,在精益领域:
Once again, in Lean:
variables A B : Prop
variable hA : A
variable hnB : ¬B
example : ¬ (A → B) :=
assume h : A → B,
have hB : B, from h hA,
show false, from hnB hB
variables A B : Prop
variable hA : A
variable hnB : ¬B
example : ¬ (A → B) :=
assume h : A → B,
have hB : B, from h hA,
show false, from hnB hB
现在我们已经定义了任何公式相对于真值赋值的真值,我们可以回答第一个语义问题了:给定一个赋值
Now that we have defined the truth of any formula relative to a truth assignment, we can answer our first semantic question: given an assignment
我们也可以反过来:给定一个公式,我们可以尝试找到一个使其为真(或为假)的真值赋值。事实上,我们可以使用 Lean 来为我们评估公式。在下面的例子中,你可以为命题符号A、B、C、D和分配任意一组值E。当你在这个输入上运行 Lean 时,语句的输出eval就是表达式的值。
We can also go in the other direction: given a formula, we can attempt to find a truth assignment that will make it true (or false). In fact, we can use Lean to evaluate formulas for us. In the example that follows, you can assign any set of values to the proposition symbols A, B, C, D, and E. When you run Lean on this input, the output of the eval statement is the value of the expression.
-- Define your truth assignment here
def A := tt
def B := ff
def C := tt
def D := tt
def E := ff
def test (p : Prop) [decidable p] : string :=
if p then "true" else "false"
#eval test ((A ∧ B) ∨ ¬ C)
#eval test (A → D)
#eval test (C → (D ∨ ¬E))
#eval test (¬(A ∧ B ∧ C ∧ D))
-- Define your truth assignment here
def A := tt
def B := ff
def C := tt
def D := tt
def E := ff
def test (p : Prop) [decidable p] : string :=
if p then "true" else "false"
#eval test ((A ∧ B) ∨ ¬ C)
#eval test (A → D)
#eval test (C → (D ∨ ¬E))
#eval test (¬(A ∧ B ∧ C ∧ D))
尝试改变真值赋值,看看会发生什么。您可以在输入的末尾添加自己的公式,并对其进行求值。尝试找到使上面测试的每个公式求值为真的真值赋值。为了增加挑战性,尝试找到一个使它们同时为真的真值赋值。
Try varying the truth assignments, to see what happens. You can add your own formulas to the end of the input, and evaluate them as well. Try to find truth assignments that make each of the formulas tested above evaluate to true. For an extra challenge, try finding a single truth assignment that makes them all true at the same time.
6.2.真值表¶
6.2. Truth Tables¶
我们提出的第二和第三个语义问题比第一个问题稍微棘手一些。给定一个公式
The second and third semantic questions we asked are a little trickier than the first. Given a formula
当然,可能的真值分配的数量取决于我们正在考虑的命题字母的数量。由于每个字母有两个可能的值,
Of course, the number of possible truth assignments depends on the number of propositional letters we’re considering. Since each letter has two possible values,
我们将使用真值表来判断公式是否为真。在真值表的左侧,我们将列出当前命题字母的所有可能的真值赋值。在右侧,我们将整个公式的真值放在相应的赋值下。
We’ll use something called a truth table to figure out when, if ever, a formula is true. On the left hand side of the truth table, we’ll put all of the possible truth assignments for the present propositional letters. On the right hand side, we’ll put the truth value of the entire formula under the corresponding assignment.
首先,真值表可以用来简洁地总结我们的逻辑连接词的语义:
To begin with, truth tables can be used to concisely summarize the semantics of our logical connectives:
我们将留给你来写表格
We will leave it to you to write the table for
对于复合公式,样式大致相同。有时,在中间列中包含子公式的真值可能会有所帮助:
For compound formulas, the style is much the same. Sometimes it can be helpful to include intermediate columns with the truth values of subformulas:
通过写出公式的真值表,我们可以浏览行并查看哪些真值赋值使公式为真。如果最后一列中的所有条目都是
By writing out the truth table for a formula, we can glance at the rows and see which truth assignments make the formula true. If all the entries in the final column are
6.3.健全性和完整性¶
6.3. Soundness and Completeness¶
假设我们心中有一个固定的演绎系统,比如自然演绎。如果该系统中有命题公式的形式证明,则称该命题公式是可证明的。如果命题公式在任何真值赋值下都为真,则称其为同义反复或有效。可证明性是一个句法概念,因为它断言存在一个句法对象,即证明。有效性是一个语义概念,因为它与真值赋值和估值有关。但直观上看,这些概念应该是一致的:两者都表达了公式
Suppose we have a fixed deduction system in mind, such as natural deduction. A propositional formula is said to be provable if there is a formal proof of it in that system. A propositional formula is said to be a tautology, or valid, if it is true under any truth assignment. Provability is a syntactic notion, in that it asserts the existence of a syntactic object, namely, a proof. Validity is a semantic notion, in that it has to do with truth assignments and valuations. But, intuitively, these notions should coincide: both express the idea that a formula
每一个可证明公式都是有效的,这个说法被称为可靠性。如果
The statement that every provable formula is valid is known as soundness. If
由于我们选择推理规则和定义估值概念的方式,这两个概念应该一致的直觉是正确的。换句话说,我们为命题逻辑提出的自然演绎系统在真值表语义方面是健全和完整的。
Because of the way we have chosen our inference rules and defined the notion of a valuation, this intuition that the two notions should coincide holds true. In other words, the system of natural deduction we have presented for propositional logic is sound and complete with respect to truth-table semantics.
这些健全性和完整性的概念延伸到假设的可证明性。如果
These notions of soundness and completeness extend to provability from hypotheses. If
给定一组命题公式
Given a set of propositional formulas
请注意,根据自然演绎规则,公式
Notice that with the rules of natural deduction, a formula
证明可靠性和完整性属于元理论的范畴,因为它要求我们推理我们的推理方法。这不是本书的重点:我们更关心的是使用逻辑和真理概念,而不是确定它们的属性。但可靠性和完整性的概念在帮助我们理解逻辑概念的性质方面发挥着重要作用,因此我们将尝试在这里提供一些提示,说明为什么这些属性适用于命题逻辑。
Proving soundness and completeness belongs to the realm of metatheory, since it requires us to reason about our methods of reasoning. This is not a central focus of this book: we are more concerned with using logic and the notion of truth than with establishing their properties. But the notions of soundness and completeness play an important role in helping us understand the nature of the logical notions, and so we will try to provide some hints here as to why these properties hold for propositional logic.
证明可靠性比证明完整性更容易。我们希望证明,无论何时
Proving soundness is easier than proving completeness. We wish to show that whenever
证明完整性更难。只要证明
Proving completeness is harder. It suffices to show that if
真值表的每一行都有一个析取项。然后,我们应该能够使用每个析取项来“评估”出现在
with one disjunct for each line of the truth table. Then, we should be able to use each disjunct to “evaluate” each expression occurring in
一个更好的方法是,以一种允许我们从
A nicer way to proceed is to express the rules of natural deduction in a way that allows us to work backward from
6.4.练习¶
6.4. Exercises¶
表明
, , 和 通过写出真值表并表明它们对于所有真值分配具有相同的值,它们在逻辑上是等价的。Show that
, , and are logically equivalent, by writing out the truth table and showing that they have the same values for all truth assignments.写出真值表
。Write out the truth table for
.表明
和 是等效的,通过写出真值表并表明它们对于所有真值分配具有相同的值。Show that
and are equivalent, by writing out the truth tables and showing that they have the same values for all truth assignments.以下蕴涵成立吗?
通过写出真值表(抱歉,太长了)来证明你的答案。清楚地标出两个假设都成立的行。
Does the following entailment hold?
Justify your answer by writing out the truth table (sorry, it is long). Indicate clearly the rows where both hypotheses come out true.
以下公式可以推导吗?请用推导或反例证明你的答案。
Are the following formulas derivable? Justify your answer with either a derivation or a counterexample.
7.一阶逻辑¶
7. First Order Logic¶
命题逻辑为描述逻辑推理的一般原则提供了一个良好的开端,但还远远不够。即使在第 2 章的“恶意和爱丽丝”例子中,它的某些限制也显而易见。命题逻辑不能为我们提供表达以下一般原则的方法:如果爱丽丝和她的儿子在海滩上,那么她的儿子也和爱丽丝在一起;没有孩子比父母年长这个一般事实;或者如果某人独自一人,那么他们就不会和别人在一起这个一般事实。为了表达这样的原则,我们需要一种方式来谈论对象和个体,以及它们的属性和它们之间的关系。这些正是更具表现力的逻辑框架所提供的,该框架称为一阶逻辑,将是接下来几章的主题。
Propositional logic provides a good start at describing the general principles of logical reasoning, but it does not go far enough. Some of the limitations are apparent even in the “Malice and Alice” example from Chapter 2. Propositional logic does not give us the means to express a general principle that tells us that if Alice is with her son on the beach, then her son is with Alice; the general fact that no child is older than his or her parent; or the general fact that if someone is alone, they are not with someone else. To express principles like these, we need a way to talk about objects and individuals, as well as their properties and the relationships between them. These are exactly what is provided by a more expressive logical framework known as first-order logic, which will be the topic of the next few chapters.
7.1.函数、谓词和关系¶
7.1. Functions, Predicates, and Relations¶
考虑一些关于自然数的普通陈述:
Consider some ordinary statements about the natural numbers:
每个自然数要么是偶数,要么是奇数,但不能同时是偶数和奇数。
Every natural number is even or odd, but not both.
自然数当且仅当它能被二整除才是偶数。
A natural number is even if and only if it is divisible by two.
如果某个自然数
,是偶数,那么也是 。If some natural number,
, is even, then so is .自然数
当且仅当 很奇怪。A natural number
is even if and only if is odd.任何大于 2 的质数都是奇数。
Any prime number that is greater than 2 is odd.
对于任意三个自然数
, , 和 , 如果 分裂 和 分裂 , 然后 分裂 。For any three natural numbers
, , and , if divides and divides , then divides .
这些陈述是正确的,但我们一般不认为它们在逻辑上有效:它们依赖于对自然数、术语“偶数”和“奇数”的含义等的假设。但是,例如,一旦我们接受了第一个陈述,白宫楼梯的数量要么是偶数要么是奇数,这似乎是一个合乎逻辑的结果,特别是,如果不是偶数,那就是奇数。为了理解这样的推论,我们需要一个能够处理对象、它们的属性以及它们之间的关系的逻辑系统。
These statements are true, but we generally do not think of them as logically valid: they depend on assumptions about the natural numbers, the meaning of the terms “even” and “odd,” and so on. But once we accept the first statement, for example, it seems to be a logical consequence that the number of stairs in the White House is either even or odd, and, in particular, if it is not even, it is odd. To make sense of inferences like these, we need a logical system that can deal with objects, their properties, and relations between them.
一阶逻辑不是一劳永逸地解决单一语言问题,而是允许我们为任何给定的感兴趣领域指定我们希望使用的符号。在本节中,我们将使用以下运行示例:
Rather than fix a single language once and for all, first-order logic allows us to specify the symbols we wish to use for any given domain of interest. In this section, we will use the following running example:
感兴趣的领域是自然数,
。The domain of interest is the natural numbers,
.有物体,
, , , ,…。There are objects,
, , , , ….这个域上有函数、加法和乘法,还有平方函数。
There are functions, addition and multiplication, as well as the square function, on this domain.
这个领域有谓词“偶数”,“奇数”和“素数”。
There are predicates on this domain, “even,” “odd,” and “prime.”
该领域的元素之间存在“等于”,“小于”和“除”的关系。
There are relations between elements of this domain, “equal,” “less than”, and “divides.”
对于我们的逻辑语言,我们将选择符号 1、2、3,
For our logical language, we will choose symbols 1, 2, 3,
函数可以接受不同数量的参数:如果
和 是自然数,那么写成 和 。 所以 接受两个参数,并且 仅需一个。Functions can take different numbers of arguments: if
and are natural numbers, it makes sense to write and . So takes two arguments, and takes only one.谓词和关系也可以用这些术语来理解。谓词
和 接受一个参数,而二元关系 和 采取两个参数。Predicates and relations can also be understood in these terms. The predicates
and take one argument, while the binary relations and take two arguments.函数与谓词不同!函数接受一个或多个参数,并返回一个值。谓词接受一个或多个参数,并且结果要么为真,要么为假。我们可以将谓词视为返回命题,而不是值。
Functions are different from predicates! A function takes one or more arguments, and returns a value. A predicate takes one or more arguments, and is either true or false. We can think of predicates as returning propositions, rather than values.
事实上,我们可以认为常数符号
作为特殊类型的函数符号,它们接受零个参数。类似地,我们可以将接受零个参数的谓词视为常量逻辑值, 和 。In fact, we can think of the constant symbols
as special sorts of function symbols that take zero arguments. Analogously, we can consider the predicates that take zero arguments to be the constant logical values, and .在普通数学中,我们经常使用“中缀”符号来表示二元函数和关系。例如,我们通常写成
或者 而不是 ,我们写 而不是 。在自然演绎中编写证明时,我们将使用这些约定,它们在 Lean 中也得到支持。In ordinary mathematics, we often use “infix” notation for binary functions and relations. For example, we usually write
or instead of , and we write instead of . We will use these conventions when writing proofs in natural deduction, and they are supported in Lean as well.我们将处理平等关系,
,作为包含在每个一阶语言中的特殊二元关系。We will treat the equality relation,
, as a special binary relation that is included in every first-order language.
一阶逻辑允许我们用基本表达式构建复杂表达式。从变量和常量开始,我们可以使用函数符号构建如下复合表达式:
First-order logic allows us to build complex expressions out of the basic ones. Starting with the variables and constants, we can use the function symbols to build up compound expressions like these:
此类表达被称为“术语”。直观上,它们命名了预期论域中的对象。
Such expressions are called “terms.” Intuitively, they name objects in the intended domain of discourse.
现在,使用谓词和关系符号,我们可以对这些表达式做出断言:
Now, using the predicates and relation symbols, we can make assertions about these expressions:
更有趣的是,我们可以使用命题连接词来构建如下复合表达式:
Even more interestingly, we can use propositional connectives to build compound expressions like these:
例如,第二个断言
The second one, for example, asserts that either
7.2.通用量词¶
7.2. The Universal Quantifier¶
一阶逻辑的强大之处在于它允许我们使用量词做出一般性断言。全称量词
What makes first-order logic powerful is that it allows us to make general assertions using quantifiers. The universal quantifier
通常会将多个相同类型的量词组合起来,例如:
It is common to combine multiple quantifiers of the same kind, and write, for example,
以下是一些有关语法的说明:
Here are some notes on syntax:
在符号逻辑中,全称量词通常被认为具有紧密结合性。例如,
被解释为 ,我们会写 以扩大范围。In symbolic logic, the universal quantifier is usually taken to bind tightly. For example,
is interpreted as , and we would write to extend the scope.但要小心。在其他情况下,尤其是在计算机科学中,人们通常会赋予量词尽可能宽的范围。精益就是这种情况。例如,被解释为,我们会写成限制范围。
∀ x, P ∨ Q∀ x, (P ∨ Q)(∀ x, P) ∨ QBe careful, however. In other contexts, especially in computer science, people often give quantifiers the widest scope possible. This is the case with Lean. For example,
∀ x, P ∨ Qis interpreted as∀ x, (P ∨ Q), and we would write(∀ x, P) ∨ Qto limit the scope.当你把量词
前面有一个涉及变量的公式 ,该变量的所有出现都受量词约束。例如,表达式 表示每个数字都是偶数或奇数。请注意变量 在非正式声明中没有出现。该声明不是关于 一点也不;而是 是一个虚拟变量,一个占位符,代表短语中提到的“事物”,该短语以“每件事”一词开头。我们想到这个表达式 与表达式相同 .Lean 也把这些表达视为相同。When you put the quantifier
in front a formula that involves the variable , all the occurrences of that variable are bound by the quantifier. For example, the expression is expresses that every number is even or odd. Notice that the variable does not appear anywhere in the informal statement. The statement is not about at all; rather is a dummy variable, a placeholder that stands for the “thing” referred to within a phrase that beings with the words “every thing.” We think of the expression as being the same as the expression . Lean also treats these expressions as the same.在精益中,表达式被解释为,右边带有括号。因此,表达式中全称量词后面的部分可以解释为“假设 除以 ,并且除以,则除以。”该表达式在逻辑上等同于,但我们将会看到,在精益中,将这样的事实表达为迭代蕴涵通常很方便。
∀ x y z, x ∣ y → y ∣ z → x ∣ z∀ x y z, x ∣ y → (y ∣ z → x ∣ z)xyyzxz∀ x y z, x ∣ y ∧ y ∣ z → x ∣ zIn Lean, the expression
∀ x y z, x ∣ y → y ∣ z → x ∣ zis interpreted as∀ x y z, x ∣ y → (y ∣ z → x ∣ z), with parentheses associated to the right. The part of the expression after the universal quantifier can therefore be interpreted as saying “given thatxdividesyand thatydividesz,xdividesz.” The expression is logically equivalent to∀ x y z, x ∣ y ∧ y ∣ z → x ∣ z, but we will see that, in Lean, it is often convenient to express facts like this as an iterated implication.
不受约束的变量称为自由变量。请注意,一阶逻辑中的公式会说明其自由变量的情况。例如,在我们想到的解释中,公式
A variable that is not bound is called free. Notice that formulas in first-order logic say things about their free variables. For example, in the interpretation we have in mind, the formula
还要注意一些公式,例如
Notice also that some formulas, like
在第 1 章中,我们证明了 2 的平方根是无理数。解释该陈述的一种方式如下:
In Chapter 1 we proved that the square root of two is irrational. One way to construe the statement is as follows:
对于每一对整数,
和 , 如果 ,事实并非如此 。
For every pair of integers,
and , if , it is not the case that .
这种表述的优点是,我们可以将注意力限制在整数上,而不必考虑更大的有理数域。在符号逻辑中,假设我们想要讨论的域是整数,我们将使用全称量词来表达这个定理:
The advantage of this formulation is that we can restrict our attention to the integers, without having to consider the larger domain of rationals. In symbolic logic, assuming our intended domain of discourse is the integers, we would express this theorem using the universal quantifier:
请注意,我们保留了传统的数学符号
Notice that we have kept the conventional mathematical notation
让
和 为任意整数,假设 ,假设 。 …
矛盾。
Let
and be arbitrary integers, suppose , and suppose . …
Contradiction.
我们实际上在做的是证明普遍命题成立,通过表明它对“任意”值都成立
What we are really doing is proving that the universal statement holds, by showing that it holds of “arbitrary” values
注意,在取消假设之后,我们证明了
Notice that after the hypotheses are canceled, we have proved
这个例子激发了自然演绎中的以下规则:
This example motivates the following rule in natural deduction:
假如
provided
那么消去规则呢?假设我们知道每个数字都是偶数或奇数。那么,在普通的证明中,我们可以自由地断言“
What about the elimination rule? Suppose we know that every number is even or odd. Then, in an ordinary proof, we are free to assert “
在哪里
where
从某种意义上说,这感觉就像是蕴涵的消除规则;我们可以将这个假设理解为“如果
In a sense, this feels like the elimination rule for implication; we might read the hypothesis as saying “if
7.3.存在量词¶
7.3. The Existential Quantifier¶
与全称量词对偶的是存在量词,
Dual to the universal quantifier is the existential quantifier,
以下关于自然数的陈述断言某个自然数的存在:
The following statements about the natural numbers assert the existence of some natural number:
存在一个奇合数。(请记住,如果自然数大于 1 并且不是素数,则该自然数为合数。)
There exists an odd composite number. (Remember that a natural number is composite if it is greater than 1 and not prime.)
每个大于一的自然数都有一个质因数。
Every natural number greater than one has a prime divisor.
对于每一个
, 如果 素因数小于 , 然后 是复合的。For every
, if has a prime divisor smaller than , then is composite.
这些语句可以使用存在量词以一阶逻辑表达如下:
These statements can be expressed in first-order logic using the existential quantifier as follows:
在我们写完之后
After we write
我们如何证明这样的存在性陈述?假设我们想证明存在一个奇合数。为此,我们只需提出一个候选数,并证明该候选数满足所需的属性。例如,我们可以选择 15,然后证明 15 是奇数,15 是合数。当然,15 并没有什么特别之处,我们也可以使用其他数字(如 9 或 35)来证明这一点。选择哪个候选数并不重要,只要它具有所需的属性即可。
How do we prove such existential statements? Suppose we want to prove that there exists an odd composite number. To do this, we just present a candidate, and show that the candidate satisfies the required properties. For example, we could choose 15, and then show that 15 is odd and that 15 is composite. Of course, there’s nothing special about 15, and we could have proven it also using a different number, like 9 or 35. The choice of candidate does not matter, as long as it has the required property.
在自然演绎证明中它看起来像这样:
In a natural deduction proof this would look like this:
这说明了存在量词的引入规则:
This illustrates the introduction rule for the existential quantifier:
在哪里
where
那么消除规则呢?假设我们知道
What about the elimination rule? Suppose that we know that
让
为任意素数,使得 和 。 …
所以,
是复合的。
Let
be any prime such that and . …
Therefore,
is composite.
首先,我们假设
First, we assume that there is some
在自然演绎中,消去规则表达如下:
In natural deduction, the elimination rule is expressed as follows:
这里我们要求
Here we require that
有期限限制
There is a restriction on the term
违反存在量词引入规则中的限制会导致类似的问题。例如,它允许我们推导出
Violating the restriction in the introduction rule for the existential quantifier causes similar problems. For example, it allows us to derive
7.4.相对化和排序¶
7.4. Relativization and Sorts¶
在我们提出的一阶逻辑中,存在一个预期的话语对象“宇宙”,而全称量词和存在量词则涵盖了该宇宙。例如,我们可以设计一种语言来谈论居住在某个城镇的人,并有一个关系
In first-order logic as we have presented it, there is one intended “universe” of objects of discourse, and the universal and existential quantifiers range over that universe. For example, we could design a language to talk about people living in a certain town, with a relation
你应该记住,在这个阶段,
You should keep in mind that, at this stage,
是正确的,尽管在最初的解释中,它对三角恋的本质做出了令人难以置信的断言。在第 10 章中,我们将阐明这一概念:一阶逻辑的演绎规则使我们能够确定在所有解释中为真的陈述,就像命题逻辑的规则使我们能够确定在所有真值赋值下为真的陈述一样。
is true, though in the original interpretation it makes an implausible claim about the nature of love triangles. In Chapter 10, we will spell out the notion that the deductive rules of first-order logic enable us to determine the statements that are true in all interpretations, just as the rules of propositional logic enable us to determine the statements that are true under all truth assignments.
回到最初的例子,假设我们想要表达这样一个观点:在我们镇上,所有的女人都很强壮,所有的男人都很帅气。我们可以用以下两个句子来表达:
Returning to the original example, suppose we want to represent the statement that, in our town, all the women are strong and all the men are good looking. We could do that with the following two sentences:
这些都是相对化的例子。全称量词涵盖了镇上的所有人,但这种手法让我们能够使用蕴涵将我们的陈述范围分别限制在男性和女性。当我们将“大于 2 的每个素数都是奇数”表示出来时,这个技巧也派上了用场:
These are instances of relativization. The universal quantifier ranges over all the people in the town, but this device gives us a way of using implication to restrict the scope of our statements to men and women, respectively. The trick also comes into play when we render “every prime number greater than two is odd”:
我们也可以更直白地理解为“对于每一个数字
We could also read this more literally as saying “for every number
也可以将存在量词相对化,以表示“某个女人很坚强”和“某个男人很帅气”之类的事情。这些表达如下:
It is also possible to relativize the existential quantifier to say things like “some woman is strong” and “some man is good-looking.” These are expressed as follows:
请注意,虽然我们使用蕴涵来相对化全称量词,但这里我们需要使用合取而不是蕴涵。表达式
Notice that although we used implication to relativize the universal quantifier, here we need to use conjunction instead of implication. The expression
现在,假设我们正在学习几何,我们想要表达这样一个事实:给定任意两个不同的点
Now, suppose we are studying geometry, and we want to express the fact that given any two distinct points
但处理这样的谓词是繁琐的,并且有一阶逻辑的温和扩展,称为多排序一阶逻辑,它内置了一些簿记。在多排序逻辑中,可以有不同类型的对象(例如点和线),以及针对每个对象的单独变量和量词库。此外,函数符号和谓词符号的规范表明它们期望什么类型的参数,以及在函数符号的情况下,它们返回什么类型的参数。例如,我们可以选择使用变量进行排序
But dealing with such predicates is tedious, and there is a mild extension of first-order logic, called many-sorted first-order logic, which builds in some of the bookkeeping. In many-sorted logic, one can have different sorts of objects—such as points and lines—and a separate stock of variables and quantifiers ranging over each. Moreover, the specification of function symbols and predicate symbols indicates what sorts of arguments they expect, and, in the case of function symbols, what sort of argument they return. For example, we might choose to have a sort with variables
7.5.平等¶
7.5. Equality¶
在符号逻辑中,我们使用表达式
In symbolic logic, we use the expression
然而,谈论“平等”或“身份”会引发复杂的哲学问题。我和三天前的我还是同一个人吗?我书架上的两本《哈克贝利·费恩历险记》是同一本书,还是两本不同的书?使用符号逻辑来模拟身份的前提是,我们心中有一种划分和解释世界的方式。我们假设我们的术语指的是不同的实体,而写作
Talk of “equality” or “identity” raises messy philosophical questions, however. Am I the same person I was three days ago? Are the two copies of Huckleberry Finn sitting on my shelf the same book, or two different books? Using symbolic logic to model identity presupposes that we have in mind a certain way of carving up and interpreting the world. We assume that our terms refer to distinct entities, and writing
反身性:
,对于任何项reflexivity:
, for any term对称性:如果
, 然后symmetry: if
, then传递性:如果
和 , 然后transitivity: if
and , then
然而,这些性质不足以表征相等性。如果两个表达式表示相同的事物,那么我们应该能够用一个表达式替换任何表达式中的其他表达式。采用以下约定很方便:如果
These properties are not enough to characterize equality, however. If two expressions denote the same thing, then we should be able to substitute one for any other in any expression. It is convenient to adopt the following convention: if
我们已经对公式采用了类似的惯例:如果我们引入一个公式
We already adopted a similar convention for formulas: if we introduce a formula as
这里,第一条替换规则控制项,第二条替换规则控制公式。在下一章中,您将学习如何使用它们。
Here, the first substitution rule governs terms and the second substitution rule governs formulas. In the next chapter, you will learn how to use them.
使用相等性,我们可以定义更多的量词。
Using equality, we can define even more quantifiers.
我们可以表达“至少有两个元素
使得 持有”为 。We can express “there are at least two elements
such that holds” as .我们可以表达“最多有两个元素
使得 持有”为 这表明如果我们有三个元素 为此 成立,那么其中两个一定相等。We can express “there are at most two elements
such that holds” as . This states that if we have three elements for which holds, then two of them must be equal.我们可以表达“恰好有两个元素
使得 成立”作为上述两个语句的合取。We can express “there are exactly two elements
such that holds” as the conjunction of the above two statements.
作为练习,用一阶逻辑写出至少、最多和恰好三个元素的陈述
As an exercise, write out in first order logic the statements that there are at least, at most, and exactly three elements
在逻辑上,表达式
In logic, the expression
第一个合取词表示至少有一个对象满足
The first conjunct says that there is at least one object satisfying
你应该思考一下为什么第二个表达式有效。在下一章中,我们将看到,使用自然演绎规则,我们可以证明这两个表达式是等价的。
You should think about why this second expression works. In the next chapter we will see that, using the rules of natural deduction, we can prove that these two expressions are equivalent.
7.6.练习¶
7.6. Exercises¶
完美数是指等于其真因数之和的数,真因数是指除其本身之外的除其本身以外的所有数。例如,6 是完美的,因为
。使用具有自然数变量和适当函数及谓词的语言,写下断言以下内容的一阶句子。使用谓词
来表达一个数字是完美的。28 是完美的。
100 至 200 之间没有完美数字。
200 到 10,000 之间至少有两个完美数字。(可以用完美数字来表达这一点
和 200 至 10,000 之间,具有以下属性 。每个完美数都是偶数。
对于每个数字,都有一个比它更大的完美数字。(这是表达“有无数个完美数字”的一种方式。)
这里,“之间”一词
和 ”旨在包括 和 。顺便说一句,我们不知道最后两个说法是否正确。这些都是悬而未决的问题。
A perfect number is a number that is equal to the sum of its proper divisors, that is, the numbers that divide it, other than itself. For example, 6 is perfect, because
.Using a language with variables ranging over the natural numbers and suitable functions and predicates, write down first-order sentences asserting the following. Use a predicate
to express that a number is perfect.28 is perfect.
There are no perfect numbers between 100 and 200.
There are (at least) two perfect numbers between 200 and 10,000. (Express this by saying that there are perfect numbers
and between 200 and 10,000, with the property that .)Every perfect number is even.
For every number, there is a perfect number that is larger than it. (This is one way to express the statement that there are infinitely many perfect numbers.)
Here, the phrase “between
and ” is meant to include and .By the way, we do not know whether the last two statements are true. They are open questions.
使用包含跨人群变量和谓词的语言
, , , , , 和 ,写下断言以下内容的一阶句子:没有人信任政客。
任何信任政客的人都是疯子。
每个人都认识某个与某位政治家有关系的人。
每个富人要么是政客,要么认识政客。
每种情况都可能涉及一些解释。请注意,写下合乎逻辑的表达式是帮助澄清含义的一种方法。
Using a language with variables ranging over people, and predicates
, , , , , and , write down first-order sentences asserting the following:Nobody trusts a politician.
Anyone who trusts a politician is crazy.
Everyone knows someone who is related to a politician.
Everyone who is rich is either a politician or knows a politician.
In each case, some interpretation may be involved. Notice that writing down a logical expression is one way of helping to clarify the meaning.
8.一阶逻辑的自然演绎¶
8. Natural Deduction for First Order Logic¶
8.1.推理规则¶
8.1. Rules of Inference¶
在上一章中,我们讨论了一阶逻辑的语言及其使用规则。我们在这里总结一下:
In the last chapter, we discussed the language of first-order logic, and the rules that govern their use. We summarize them here:
全称量词:
The universal quantifier:
在介绍规则中,
In the introduction rule,
存在量词:
The existential quantifier:
在介绍规则中,
In the introduction rule,
平等:
Equality:
严格来说,只有
Strictly speaking, only
8.2.通用量词¶
8.2. The Universal Quantifier¶
以下自然演绎证明示例表明,如果对于每个
The following example of a proof in natural deduction shows that if, for every
请注意,假设 1 和假设 2 都没有提到
Notice that neither of the assumptions 1 or 2 mention
下面是另一个示例:
Here is another example:
作为练习,尝试证明以下内容:
As an exercise, try proving the following:
这是一个更具挑战性的练习。假设我告诉你,在一个城镇里,有一位(男性)理发师,他只为所有不自己刮胡子的男士刮胡子。你可以证明这是一个矛盾,非正式的论证如下:
Here is a more challenging exercise. Suppose I tell you that, in a town, there is a (male) barber that shaves all and only the men who do not shave themselves. You can show that this is a contradiction, arguing informally, as follows:
根据假设,理发师给自己刮胡子当且仅当他不给自己刮胡子。将此语句称为 (*)。
假设理发师给自己刮胡子。根据 (*),这意味着他不给自己刮胡子,这是一个矛盾。所以,理发师不给自己刮胡子。
但是再次使用 (*),这意味着理发师给自己刮胡子,这与我们刚才证明的事实相矛盾,即理发师不给自己刮胡子。
By the assumption, the barber shaves himself if and only if he does not shave himself. Call this statement (*).
Suppose the barber shaves himself. By (*), this implies that he does not shave himself, a contradiction. So, the barber does not shave himself.
But using (*) again, this implies that the barber shaves himself, which contradicts the fact we just showed, namely, that the barber does not shave himself.
尝试将其转化为自然推理的正式论证。
Try to turn this into a formal argument in natural deduction.
让我们回到自然数的例子,看看演绎概念是如何发挥作用的。假设我们定义了
Let us return to the example of the natural numbers, to see how deductive notions play out there. Suppose we have defined
然后我们可以继续推导
Then we can go on to derive
我们还可以证明
We can also prove and
随着我们从建模基本推理规则转向建模实际数学证明,我们将倾向于将重点从自然演绎转向 Lean 中的形式化证明。自然演绎有其用途:作为逻辑推理的模型,它为我们提供了一种研究元理论属性(如健全性和完整性)的便捷方法。然而,对于在系统内工作,像 Lean 这样的证明语言往往具有更好的可扩展性,并能产生更易读的证明。
As we move from modeling basic rules of inference to modeling actual mathematical proofs, we will tend to shift focus from natural deduction to formal proofs in Lean. Natural deduction has its uses: as a model of logical reasoning, it provides us with a convenient means to study metatheoretic properties such as soundness and completeness. For working within the system, however, proof languages like Lean’s tend to scale better, and produce more readable proofs.
8.3.存在量词¶
8.3. The Existential Quantifier¶
请记住,存在量词消除规则背后的直觉是,如果我们知道
Remember that the intuition behind the elimination rule for the existential quantifier is that if we know
以下证明表明,如果存在满足
The following proof shows that if there is something satisfying either
下面的例子更加复杂:
The following example is more involved:
在这个证明中,存在消除规则(标有
In this proof, the existential elimination rule (the line labeled
另一个例子是
Another example is that if
这很短但很棘手,所以让我们仔细研究一下。在左边,我们假设
This is short but tricky, so let us go through it carefully. On the left, we assume
8.4.平等¶
8.4. Equality¶
回想一下平等的自然演绎规则:
Recall the natural deduction rules for equality:
请记住,我们已经隐式地固定了一些一阶语言,并且
Keep in mind that we have implicitly fixed some first-order language, and
这条线上的第二个公理是类似的,只不过现在
The second axiom on that line is similar, except now
请注意,我们已经写了反身性公理,
Notice that we have written the reflexivity axiom,
事实上,我们可以把第二行的第一个推论看作是第二个推论的一个特例。例如,考虑公式
In fact, we can think of the first inference on the second line as a special case of the second one. Consider, for example, the formula
粗略地说,我们正在替换第二个实例
Roughly speaking, we are replacing the second instance of
等式规则让我们能够用符号逻辑进行计算。这通常相当于使用我们已经讨论过的等式规则以及一系列一般恒等式。例如,以下恒等式适用于任何实数
Equality rules let us carry out calculations in symbolic logic. This typically amounts to using the equality rules we have already discussed, together with a list of general identities. For example, the following identities hold for any real numbers
加法的交换性:
commutativity of addition:
加法的结合律:
associativity of addition:
加法恒等式:
additive identity:
加法逆元:
additive inverse:
乘法恒等式:
multiplicative identity:
乘法交换性:
commutativity of multiplication:
乘法的结合律:
associativity of multiplication:
分配性:
distributivity:
你应该想象每个语句前面都有隐式的全称量词,断言该语句对任何值都成立
You should imagine that there are implicit universal quantifiers in front of each statement, asserting that the statement holds for any values of
该策略是使用全称量词的消去规则来实例化一般恒等式,在必要时使用对称性将方程导向正确的方向,然后使用等式的替换规则来改变先前结果中的某些内容。例如,这是一个简单恒等式的自然演绎证明,
The strategy is to use the elimination rule for the universal quantifier to instantiate general identities, use symmetry, if necessary, to orient an equation in the right direction, and then using the substitution rule for equality to change something in a previous result. For example, here is a natural deduction proof of a simple identity,
通常,通过自然演绎进行此类计算不会学到什么有趣的东西,但你应该尝试一两个例子来掌握它,然后高兴地知道它是可能的。
There is generally nothing interesting to be learned from carrying out such calculations in natural deduction, but you should try one or two examples to get the hang of it, and then take pleasure in knowing that it is possible.
8.5.反例和相对量词¶
8.5. Counterexamples and Relativized Quantifiers¶
考虑以下陈述:
Consider the statement:
每个质数都是奇数。
Every prime number is odd.
在一阶逻辑中,我们可以将其表述为
In first-order logic, we could formulate this as
更一般地,给定一个公式
More generally, given a formula
关于证明的一个注释:在标记为
One remark about the proof: at the step marked by
作为练习,自己证明“对偶”等价性:
As an exercise, prove the “dual” equivalence yourself:
在第 7 章中,我们看到了如何使用相对化来限制全称量词的范围的例子。假设我们想说“每个素数都大于 1”。在一阶逻辑中,这可以写成
In Chapter 7 we saw examples of how to use relativization to restrict the scope of a universal quantifier. Suppose we want to say “every prime number is greater than 1”. In first order logic this can be written as
作为练习,你可以证明上述关于量词否定的结果也适用于相对量词。具体来说,证明以下陈述:
As an exercise you can prove the above results about negations of quantifiers also for relativized quantifiers. Specifically, prove the following statements:
作为参考,下面列出了涉及量词的有效句子:
For reference, here is a list of valid sentences involving quantifiers:
如果 不是免费的 if is not free in 如果 不是免费的 if is not free in 如果 不是免费的 if is not free in 如果 不是免费的 if is not free in 如果 不是免费的 if is not free in 如果 不是免费的 if is not free in 如果 不是免费的 if is not free in 如果 不是免费的 if is not free in
所有这些都可以通过自然演绎推导出来。最后两个规则允许我们将否定推向内部,因此我们可以继续将一阶公式置于否定范式中。其他规则允许我们将量词放在任何公式的前面,尽管一般来说,这样做有多种方法。例如,公式
All of these can be derived in natural deduction. The last two allow us to push negations inwards, so we can continue to put first-order formulas in negation normal form. Other rules allow us to bring quantifiers to the front of any formula, though, in general, there will be multiple ways of doing this. For example, the formula
相当于两者
is equivalent to both
和
and
所有量词都在前面的公式被称为前缀形式。
A formula with all the quantifiers in front is said to be in prenex form.
8.6.练习¶
8.6. Exercises¶
给出自然演绎证明
Give a natural deduction proof of
给出自然演绎证明
来自假设 和 。Give a natural deduction proof of
from hypotheses and .从假设出发
和 给出自然演绎证明 (想想 定义为函数 。From hypotheses
and give a natural deduction proof . (It might help to think of as the function defined by .)给出自然演绎证明
。Give a natural deduction proof of
.给出自然演绎证明
根据假设 和 。Give a natural deduction proof of
from the assumptions and .证明最后一节中的其他一些等价性。
Prove some of the other equivalences in the last section.
考虑一下一阶逻辑中表达“没有人相信政治家”的各种方式:
它们都是逻辑等价的。通过对每个命题给出自然演绎证明,来证明第二命题和第四命题。(作为一种捷径,在
引入和消除规则,您可以在一个步骤中引入/消除两个变量。)Consider some of the various ways of expressing “nobody trusts a politician” in first-order logic:
They are all logically equivalent. Show this for the second and the fourth, by giving natural deduction proofs of each from the other. (As a shortcut, in the
introduction and elimination rules, you can introduce / eliminate both variables in one step.)将以下陈述形式化,并给出自然的演绎证明,其中前三个陈述作为(未取消的)假设出现,最后一行是结论:
每个年轻健康的人都喜欢棒球。
每一个活跃的人都是健康的。
有人年轻,活跃。
因此,有人喜欢棒球。
使用
因为“年轻”, 对于“健康” 对于“处于活动状态”, 意思是“喜欢棒球”。Formalize the following statements, and give a natural deduction proof in which the first three statements appear as (uncancelled) hypotheses, and the last line is the conclusion:
Every young and healthy person likes baseball.
Every active person is healthy.
Someone is young and active.
Therefore, someone likes baseball.
Use
for “is young,” for “is healthy,” for “is active,” and for “likes baseball.”给出自然演绎证明
使用第 8.4 节中的相等规则。Give a natural deduction proof of
using the equality rules in Section 8.4.给出自然演绎证明
仅使用这两个假设(而不使用任何新的相等规则):(提示:选择实例
, , 和 小心一点。你可以像上次的家庭作业一样,一步就实例化所有全称量词。)Give a natural deduction proof of
using only these two hypotheses (and none of the new equality rules):(Hint: Choose instantiations of
, , and carefully. You can instantiate all the universal quantifiers in one step, as on the last homework assignment.)给出自然演绎证明
Give a natural deduction proof of
给出自然演绎证明
Give a natural deduction proof of
请记住,以下两种表达
也就是说,存在一个唯一的 令人满意 :执行以下操作:
以第一点为假设,对第二点给出自然的演绎证明。
对第一个问题给出自然的演绎证明,并将第二个问题作为假设。
(警告:这些很长。)
Remember that both the following express
, that is, the statement that there is a unique satisfying :Do the following:
Give a natural deduction proof of the second, assuming the first as a hypothesis.
Give a natural deduction proof of the first, asssuming the second as a hypothesis.
(Warning: these are long.)
9.精益中的一阶逻辑¶
9. First Order Logic in Lean¶
9.1.函数、谓词和关系¶
9.1. Functions, Predicates, and Relations¶
在上一章中,我们讨论了一阶逻辑的语言。在本书中,我们将看到 Lean 的内置逻辑更具表现力;但它包括一阶逻辑,也就是说,任何可以用一阶逻辑表达(和证明)的东西都可以用 Lean 表达(和证明)。
In the last chapter, we discussed the language of first-order logic. We will see in the course of this book that Lean’s built-in logic is much more expressive; but it includes first-order logic, which is to say, anything that can be expressed (and proved) in first-order logic can be expressed (and proved) in Lean.
精益基于一种称为类型理论的基础框架,其中每个变量都假定为某种类型的元素。从一阶逻辑的角度来看,你可以将类型视为“宇宙”或“论域”。
Lean is based on a foundational framework called type theory, in which every variable is assumed to range elements of some type. You can think of a type as being a “universe,” or a “domain of discourse,” in the sense of first-order logic.
例如,假设我们要使用具有一个常量符号、一个一元函数符号、一个二元函数符号、一个一元关系符号和一个二元关系符号的一阶语言。我们可以声明一个新类型U(表示“universe”)和相关符号,如下所示:
For example, suppose we want to work with a first-order language with one constant symbol, one unary function symbol, one binary function symbol, one unary relation symbol, and one binary relation symbol. We can declare a new type U (for “universe”) and the relevant symbols as follows:
constant U : Type
constant c : U
constant f : U → U
constant g : U → U → U
constant P : U → Prop
constant R : U → U → Prop
constant U : Type
constant c : U
constant f : U → U
constant g : U → U → U
constant P : U → Prop
constant R : U → U → Prop
然后我们可以按如下方式使用它们:
We can then use them as follows:
variables x y : U
#check c
#check f c
#check g x y
#check g x (f c)
#check P (g x (f c))
#check R x y
variables x y : U
#check c
#check f c
#check g x y
#check g x (f c)
#check P (g x (f c))
#check R x y
该#check命令告诉我们前四个表达式的类型为U,后两个表达式的类型为Prop。粗略地说,这意味着前四个表达式对应于一阶逻辑的项,后两个表达式对应于公式。
The #check command tells us that the first four expressions have type U, and that the last two have type Prop. Roughly, this means that the first four expressions correspond to terms of first-order logic, and that the last two correspond to formulas.
请注意以下所有内容:
Note all the following:
一元函数表示为类型的对象,二元函数表示为类型的对象,使用与命题之间的蕴涵相同的符号。
U → UU → U → UA unary function is represented as an object of type
U → Uand a binary function is represented as an object of typeU → U → U, using the same notation as for implication between propositions.例如,我们写为表示将应用于的结果,以及表示将应用于和 的结果,就像我们在使用肯定前件进行一阶逻辑时所做的那样。表达式中需要使用括号来确保被解析为单个参数。
f xfxg x ygxyg x (f c)f cWe write, for example,
f xto denote the result of applyingftox, andg x yto denote the result of applyinggtoxandy, again just as we did when using modus ponens for first-order logic. Parentheses are needed in the expressiong x (f c)to ensure thatf cis parsed as a single argument.一元谓词表示为 类型的对象,二元谓词表示为 类型的对象。你可以将二元关系视为一个函数,它假设宇宙中的两个参数,并返回一个命题。
U → PropU → U → PropRUA unary predicate is presented as an object of type
U → Propand a binary predicate is represented as an object of typeU → U → Prop. You can think of a binary relationRas being a function that assumes two arguments in the universe,U, and returns a proposition.我们写为表示对于 成立的断言,以及表示对于和成立。
P xPxR x yRxyWe write
P xto denote the assertion thatPholds ofx, andR x yto denote thatRholds ofxandy.
您可能想知道 Lean 中的常量和变量之间有什么区别。以下声明也有效:
You may reasonably wonder what difference there is between a constant and a variable in Lean. The following declarations also work:
variable U : Type
variable c : U
variable f : U → U
variable g : U → U → U
variable P : U → Prop
variable R : U → U → Prop
variables x y : U
#check c
#check f c
#check g x y
#check g x (f c)
#check P (g x (f c))
#check R x y
variable U : Type
variable c : U
variable f : U → U
variable g : U → U → U
variable P : U → Prop
variable R : U → U → Prop
variables x y : U
#check c
#check f c
#check g x y
#check g x (f c)
#check P (g x (f c))
#check R x y
尽管示例的功能大致相同,但constant和variable命令的作用却大不相同。constant命令公理地声明一个新对象,并将其添加到 Lean 知道的对象列表中。相比之下,首次执行时,命令variable不会创建任何东西。相反,它告诉 Lean,每当我们使用相应的标识符输入表达式时,它都应该创建一个相应类型的临时变量。
Although the examples function in much the same way, the constant and variable commands do very different things. The constant command declares a new object, axiomatically, and adds it to the list of objects Lean knows about. In contrast, when it is first executed, the variable command does not create anything. Rather, it tells Lean that whenever we enter an expression using the corresponding identifier, it should create a temporary variable of the corresponding type.
Lean 的标准库中已经声明了许多类型。例如,有一种类型写为nat或ℕ,表示自然数:
Many types are already declared in Lean’s standard library. For example, there is a type written nat or ℕ, that denotes the natural numbers:
#check nat
#check ℕ
#check nat
#check ℕ
ℕ您可以使用\nat或输入unicode \N。 这两个表达式含义相同。
You can enter the unicode ℕ with \nat or \N. The two expressions mean the same thing.
使用这种内置类型,我们可以对算术语言进行建模,如上一章所述,如下所示:
Using this built-in type, we can model the language of arithmetic, as described in the last chapter, as follows:
namespace hidden
constant mul : ℕ → ℕ → ℕ
constant add : ℕ → ℕ → ℕ
constant square : ℕ → ℕ
constant even : ℕ → Prop
constant odd : ℕ → Prop
constant prime : ℕ → Prop
constant divides : ℕ → ℕ → Prop
constant lt : ℕ → ℕ → Prop
constant zero : ℕ
constant one : ℕ
end hidden
namespace hidden
constant mul : ℕ → ℕ → ℕ
constant add : ℕ → ℕ → ℕ
constant square : ℕ → ℕ
constant even : ℕ → Prop
constant odd : ℕ → Prop
constant prime : ℕ → Prop
constant divides : ℕ → ℕ → Prop
constant lt : ℕ → ℕ → Prop
constant zero : ℕ
constant one : ℕ
end hidden
我们使用了namespace命令来避免与 Lean 库中已声明的标识符发生冲突。(在命名空间之外,mul我们刚刚声明的常量名为hidden.mul。)我们可以再次使用该#check命令来尝试它们:
We have used the namespace command to avoid conflicts with identifiers that are already declared in the Lean library. (Outside the namespace, the constant mul we just declared is named hidden.mul.) We can again use the #check command to try them out:
namespace hidden
constant mul : ℕ → ℕ → ℕ
constant add : ℕ → ℕ → ℕ
constant square : ℕ → ℕ
constant even : ℕ → Prop
constant odd : ℕ → Prop
constant prime : ℕ → Prop
constant divides : ℕ → ℕ → Prop
constant lt : ℕ → ℕ → Prop
constant zero : ℕ
constant one : ℕ
variables w x y z : ℕ
#check mul x y
#check add x y
#check square x
#check even x
end hidden
namespace hidden
constant mul : ℕ → ℕ → ℕ
constant add : ℕ → ℕ → ℕ
constant square : ℕ → ℕ
constant even : ℕ → Prop
constant odd : ℕ → Prop
constant prime : ℕ → Prop
constant divides : ℕ → ℕ → Prop
constant lt : ℕ → ℕ → Prop
constant zero : ℕ
constant one : ℕ
variables w x y z : ℕ
#check mul x y
#check add x y
#check square x
#check even x
end hidden
我们甚至可以声明二元运算和关系的中缀表示法:
We can even declare infix notation of binary operations and relations:
local infix + := add
local infix * := mul
local infix < := lt
local infix + := add
local infix * := mul
local infix < := lt
1(获取数字、2、 、 ...的符号3比较棘手。)有了所有这些,上面的例子可以呈现如下:
(Getting notation for numerals 1, 2, 3, … is trickier.) With all this in place, the examples above can be rendered as follows:
#check even (x + y + z) ∧ prime ((x + one) * y * y)
#check ¬ (square (x + y * z) = w) ∨ x + y < z
#check x < y ∧ even x ∧ even y → x + one < y
#check even (x + y + z) ∧ prime ((x + one) * y * y)
#check ¬ (square (x + y * z) = w) ∨ x + y < z
#check x < y ∧ even x ∧ even y → x + one < y
事实上,这里讨论的所有函数、谓词和关系(除“square”函数外)都是在 Lean 库中定义的。当我们将命令和放在 Lean 中的文件顶部时,它们就可供我们使用。import data.nat.primeopen nat
In fact, all of the functions, predicates, and relations discussed here, except for the “square” function, are defined in the Lean library. They become available to us when we put the commands import data.nat.prime and open nat at the top of a file in Lean.
import data.nat.prime
open nat
constant square : ℕ → ℕ
variables w x y z : ℕ
#check even (x + y + z) ∧ prime ((x + 1) * y * y)
#check ¬ (square (x + y * z) = w) ∨ x + y < z
#check x < y ∧ even x ∧ even y → x + 1 < y
import data.nat.prime
open nat
constant square : ℕ → ℕ
variables w x y z : ℕ
#check even (x + y + z) ∧ prime ((x + 1) * y * y)
#check ¬ (square (x + y * z) = w) ∨ x + y < z
#check x < y ∧ even x ∧ even y → x + 1 < y
在这里,我们公理化地声明常量square和prime,但引用 Lean 库中的其他操作和谓词。在本书中,我们通常会以这种方式进行,明确告诉您应该使用库中的哪些事实进行练习。
Here, we declare the constants square and prime axiomatically, but refer to the other operations and predicates in the Lean library. In this book, we will often proceed in this way, telling you explicitly what facts from the library you should use for exercises.
再次注意语法的以下几个方面:
Again, note the following aspects of syntax:
与普通数学符号不同,在精益中,函数的应用无需括号或逗号。例如,我们写成
square xadd x y 和 。In contrast to ordinary mathematical notation, in Lean, functions are applied without parentheses or commas. For example, we write
square xandadd x yinstead of and .谓词和关系也是如此:我们写成
even xlt x y 和 ,就像在符号逻辑中所做的那样。The same holds for predicates and relations: we write
even xandlt x yinstead of and , as one might do in symbolic logic.该符号表示加法假设两个参数,都是自然数,并返回一个自然数。
add : ℕ → ℕ → ℕThe notation
add : ℕ → ℕ → ℕindicates that addition assumes two arguments, both natural numbers, and returns a natural number.类似地,符号表示是二元关系,它假设两个自然数作为参数并形成一个命题。换句话说,表达了除以的断言。
divides : ℕ → ℕ → Propdividesdivides x yxySimilarly, the notation
divides : ℕ → ℕ → Propindicates thatdividesis a binary relation, which assumes two natural numbers as arguments and forms a proposition. In other words,divides x yexpresses the assertion thatxdividesy.
Lean 可以帮助我们区分术语和公式。如果我们使用Lean 中的#check表达式,我们会被告知它具有类型,也就是说,它表示一个自然数。如果我们使用表达式,我们会被告知它具有类型,也就是说,它表达了一个命题。x + y + 1ℕ#checkeven (x + y + 1)Prop
Lean can help us distinguish between terms and formulas. If we #check the expression x + y + 1 in Lean, we are told it has type ℕ, which is to say, it denotes a natural number. If we #check the expression even (x + y + 1), we are told that it has type Prop, which is to say, it expresses a proposition.
在第 7 章中,我们考虑了多类别逻辑,其中可以有多个领域。例如,我们可能希望将一阶逻辑用于几何,量词的范围包括点和线。在精益中,我们可以通过为每种类别引入一种新类型来建模:
In Chapter 7 we considered many-sorted logic, where one can have multiple universes. For example, we might want to use first-order logic for geometry, with quantifiers ranging over points and lines. In Lean, we can model this as by introducing a new type for each sort:
variables Point Line : Type
variable lies_on : Point → Line → Prop
variables Point Line : Type
variable lies_on : Point → Line → Prop
然后我们可以表达两个不同的点确定一条线,如下所示:
We can then express that two distinct points determine a line as follows:
#check ∀ (p q : Point) (L M : Line),
p ≠ q → lies_on p L → lies_on q L → lies_on p M →
lies_on q M → L = M
#check ∀ (p q : Point) (L M : Line),
p ≠ q → lies_on p L → lies_on q L → lies_on p M →
lies_on q M → L = M
请注意,我们遵循了在先行词中使用迭代蕴涵而不是合取的惯例。事实上,Lean 足够聪明,可以根据它们与关系一起使用的事实推断出p、q、L和是什么类型的对象,因此我们可以更简单地写成这样:Mlies_on
Notice that we have followed the convention of using iterated implication rather than conjunction in the antecedent. In fact, Lean is smart enough to infer what sorts of objects p, q, L, and M are from the fact that they are used with the relation lies_on, so we could have written, more simply, this:
#check ∀ p q L M, p ≠ q → lies_on p L → lies_on q L →
lies_on p M → lies_on q M → L = M
#check ∀ p q L M, p ≠ q → lies_on p L → lies_on q L →
lies_on p M → lies_on q M → L = M
9.2.使用全称量词¶
9.2. Using the Universal Quantifier¶
在精益中,你可以通过 来输入全称量词。第 7.1 节\all中的激励示例如下所示:
In Lean, you can enter the universal quantifier by writing \all. The motivating examples from Section 7.1 are rendered as follows:
import data.nat.prime
open nat
#check ∀ x, (even x ∨ odd x) ∧ ¬ (even x ∧ odd x)
#check ∀ x, even x ↔ 2 ∣ x
#check ∀ x, even x → even (x^2)
#check ∀ x, even x ↔ odd (x + 1)
#check ∀ x, prime x ∧ x > 2 → odd x
#check ∀ x y z, x ∣ y → y ∣ z → x ∣ z
import data.nat.prime
open nat
#check ∀ x, (even x ∨ odd x) ∧ ¬ (even x ∧ odd x)
#check ∀ x, even x ↔ 2 ∣ x
#check ∀ x, even x → even (x^2)
#check ∀ x, even x ↔ odd (x + 1)
#check ∀ x, prime x ∧ x > 2 → odd x
#check ∀ x y z, x ∣ y → y ∣ z → x ∣ z
请记住,Lean 要求全称量词后面有一个逗号,并赋予其尽可能宽的范围。例如,被解释为,我们会写成来限制范围。如果您愿意,可以使用纯 ascii 表达式代替 unicode 。∀ x, P ∨ Q∀ x, (P ∨ Q)(∀ x, P) ∨ Qforall∀
Remember that Lean expects a comma after the universal quantifier, and gives it the widest scope possible. For example, ∀ x, P ∨ Q is interpreted as ∀ x, (P ∨ Q), and we would write (∀ x, P) ∨ Q to limit the scope. If you prefer, you can use the plain ascii expression forall instead of the unicode ∀.
在精益中,证明通用陈述的模式如下:
In Lean, then, the pattern for proving a universal statement is rendered as follows:
variable U : Type
variable P : U → Prop
example : ∀ x, P x :=
assume x,
show P x, from sorry
variable U : Type
variable P : U → Prop
example : ∀ x, P x :=
assume x,
show P x, from sorry
读作“固定的任意值”。由于我们可以随意重命名绑定变量,因此我们可以等效地编写以下任一代码:assume xxU
Read assume x as “fix an arbitrary value x of U.” Since we are allowed to rename bound variables at will, we can equivalently write either of the following:
variable U : Type
variable P : U → Prop
example : ∀ y, P y :=
assume x,
show P x, from sorry
example : ∀ x, P x :=
assume y,
show P y, from sorry
variable U : Type
variable P : U → Prop
example : ∀ y, P y :=
assume x,
show P x, from sorry
example : ∀ x, P x :=
assume y,
show P y, from sorry
这构成了全称量词的引入规则。它与蕴涵的引入规则非常相似:我们不是使用assume来暂时引入一个假设,而是使用assume来暂时引入一个新对象。y(事实上,assume和assume都是 Lean 中单个内部构造的替代语法,也可以用 表示λ。)
This constitutes the introduction rule for the universal quantifier. It is very similar to the introduction rule for implication: instead of using assume to temporarily introduce an assumption, we use assume to temporarily introduce a new object, y. (In fact, assume and assume are both alternate syntax for a single internal construct in Lean, which can also be denoted by λ.)
类似地,消除规则的实施方式如下:
The elimination rule is, similarly, implemented as follows:
variable U : Type
variable P : U → Prop
variable h : ∀ x, P x
variable a : U
example : P a :=
show P a, from h a
variable U : Type
variable P : U → Prop
variable h : ∀ x, P x
variable a : U
example : P a :=
show P a, from h a
观察符号:是通过将假设“应用”于得到的。再次注意与蕴涵消去规则的相似性。P aha
Observe the notation: P a is obtained by “applying” the hypothesis h to a. Once again, note the similarity to the elimination rule for implication.
以下是使用方法的示例:
Here is an example of how it is used:
variable U : Type
variables A B : U → Prop
example (h1 : ∀ x, A x → B x) (h2 : ∀ x, A x) : ∀ x, B x :=
assume y,
have h3 : A y, from h2 y,
have h4 : A y → B y, from h1 y,
show B y, from h4 h3
variable U : Type
variables A B : U → Prop
example (h1 : ∀ x, A x → B x) (h2 : ∀ x, A x) : ∀ x, B x :=
assume y,
have h3 : A y, from h2 y,
have h4 : A y → B y, from h1 y,
show B y, from h4 h3
以下是相同证明的更短版本,我们避免使用have:
Here is an even shorter version of the same proof, where we avoid using have:
example (h1 : ∀ x, A x → B x) (h2 : ∀ x, A x) : ∀ x, B x :=
assume y,
show B y, from h1 y (h2 y)
example (h1 : ∀ x, A x → B x) (h2 : ∀ x, A x) : ∀ x, B x :=
assume y,
show B y, from h1 y (h2 y)
你应该在这里讲解这些步骤。将 应用于h1得到y的证明,然后我们将其应用于,这是 的证明。结果就是我们想要的 的证明。A y → B yh2 yA yB y
You should talk through the steps, here. Applying h1 to y yields a proof of A y → B y, which we then apply to h2 y, which is a proof of A y. The result is the proof of B y that we are after.
在上一章中,我们考虑了自然演绎中的以下证明:
In the last chapter, we considered the following proof in natural deduction:
以下是在 Lean 中呈现的相同证明:
Here is the same proof rendered in Lean:
variable U : Type
variables A B : U → Prop
example : (∀ x, A x) → (∀ x, B x) → (∀ x, A x ∧ B x) :=
assume hA : ∀ x, A x,
assume hB : ∀ x, B x,
assume y,
have Ay : A y, from hA y,
have By : B y, from hB y,
show A y ∧ B y, from and.intro Ay By
variable U : Type
variables A B : U → Prop
example : (∀ x, A x) → (∀ x, B x) → (∀ x, A x ∧ B x) :=
assume hA : ∀ x, A x,
assume hB : ∀ x, B x,
assume y,
have Ay : A y, from hA y,
have By : B y, from hB y,
show A y ∧ B y, from and.intro Ay By
以下是另一个版本,使用了“匿名”版本have:
Here is an alternative version, using the “anonymous” versions of have:
variable U : Type
variables A B : U → Prop
example : (∀ x, A x) → (∀ x, B x) → (∀ x, A x ∧ B x) :=
assume hA : ∀ x, A x,
assume hB : ∀ x, B x,
assume y,
have A y, from hA y,
have B y, from hB y,
show A y ∧ B y, from and.intro ‹A y› ‹B y›
variable U : Type
variables A B : U → Prop
example : (∀ x, A x) → (∀ x, B x) → (∀ x, A x ∧ B x) :=
assume hA : ∀ x, A x,
assume hB : ∀ x, B x,
assume y,
have A y, from hA y,
have B y, from hB y,
show A y ∧ B y, from and.intro ‹A y› ‹B y›
下面的练习要求你证明上一章讨论过的理发师悖论。你可以只使用命题推理和我们刚刚讨论过的全称量词规则来证明。
The exercises below ask you to prove the barber paradox, which was discussed in the last chapter. You can do that using only propositional reasoning and the rules for the universal quantifier that we have just discussed.
9.3.使用存在量词¶
9.3. Using the Existential Quantifier¶
∃在 Lean 中,您可以通过书写 来输入存在量词\ex。如果您愿意,可以使用 ascii 等效项 。exists引入规则是exists.intro,需要两个参数:一个术语和一个术语满足所需属性的证明。
In Lean, you can type the existential quantifier, ∃, by writing \ex. If you prefer you can use the ascii equivalent, exists. The introduction rule is exists.intro and requires two arguments: a term, and a proof that term satisfies the required property.
variable U : Type
variable P : U → Prop
example (y : U) (h : P y) : ∃ x, P x :=
exists.intro y h
variable U : Type
variable P : U → Prop
example (y : U) (h : P y) : ∃ x, P x :=
exists.intro y h
存在量词的消去规则由 给出exists.elim。它遵循自然演绎规则的形式:如果我们知道并且试图证明,则只需引入一个新变量 ,并在 成立的假设下证明。∃x, P xQyQP y
The elimination rule for the existential quantifier is given by exists.elim. It follows the form of the natural deduction rule: if we know ∃x, P x and we are trying to prove Q, it suffices to introduce a new variable, y, and prove Q under the assumption that P y holds.
variable U : Type
variable P : U → Prop
variable Q : Prop
example (h1 : ∃ x, P x) (h2 : ∀ x, P x → Q) : Q :=
exists.elim h1
(assume (y : U) (h : P y),
have h3 : P y → Q, from h2 y,
show Q, from h3 h)
variable U : Type
variable P : U → Prop
variable Q : Prop
example (h1 : ∃ x, P x) (h2 : ∀ x, P x → Q) : Q :=
exists.elim h1
(assume (y : U) (h : P y),
have h3 : P y → Q, from h2 y,
show Q, from h3 h)
和往常一样,我们可以省略之后的数据类型y和假设信息,因为 Lean 可以从上下文中推断出它们。删除并用其证明替换,得到一个简短(但几乎不可读)的结论证明。hassumeshowh3h2 y
As usual, we can leave off the information as to the data type of y and the hypothesis h after the assume, since Lean can figure them out from the context. Deleting the show and replacing h3 by its proof, h2 y, yields a short (though virtually unreadable) proof of the conclusion.
example (h1 : ∃ x, P x) (h2 : ∀ x, P x → Q) : Q :=
exists.elim h1 (assume y h, h2 y h)
example (h1 : ∃ x, P x) (h2 : ∀ x, P x → Q) : Q :=
exists.elim h1 (assume y h, h2 y h)
下面的示例同时使用了存在量词的引入规则和消除规则。
The following example uses both the introduction and the elimination rules for the existential quantifier.
variable U : Type
variables A B : U → Prop
example : (∃ x, A x ∧ B x) → ∃ x, A x :=
assume h1 : ∃ x, A x ∧ B x,
exists.elim h1
(assume y (h2 : A y ∧ B y),
have h3 : A y, from and.left h2,
show ∃ x, A x, from exists.intro y h3)
variable U : Type
variables A B : U → Prop
example : (∃ x, A x ∧ B x) → ∃ x, A x :=
assume h1 : ∃ x, A x ∧ B x,
exists.elim h1
(assume y (h2 : A y ∧ B y),
have h3 : A y, from and.left h2,
show ∃ x, A x, from exists.intro y h3)
注意假设中的括号;如果我们省略它们,第一个括号之后的所有内容都将包含在该量词的范围内。从假设中,我们得到满足的,因此特别的。所以足以证明结论。∃ xyA y ∧ B yA yy
Notice the parentheses in the hypothesis; if we left them out, everything after the first ∃ x would be included in the scope of that quantifier. From the hypothesis, we obtain a y that satisfies A y ∧ B y, and hence A y in particular. So y is enough to witness the conclusion.
有时将证明放在exists.elim括号中会很烦人,就像我们在这里对块所做的那样。为了避免这种情况,我们可以使用编程世界中的一些语法,并使用美元符号代替。在 Lean 中,表达式的含义与 相同,优点是我们不必记住关闭括号。使用这个小工具,我们可以按如下方式编写上述证明:assume ... showf $ tf (t)
It is sometimes annoying to enclose the proof after an exists.elim in parenthesis, as we did here with the assume ... show block. To avoid that, we can use a bit of syntax from the programming world, and use a dollar sign instead. In Lean, an expression f $ t means the same thing as f (t), with the advantage that we do not have to remember to close the parenthesis. With this gadget, we can write the proof above as follows:
variable U : Type
variables A B : U → Prop
example : (∃ x, A x ∧ B x) → ∃ x, A x :=
assume h1 : ∃ x, A x ∧ B x,
exists.elim h1 $
assume y (h2 : A y ∧ B y),
have h3 : A y, from and.left h2,
show ∃ x, A x, from exists.intro y h3
variable U : Type
variables A B : U → Prop
example : (∃ x, A x ∧ B x) → ∃ x, A x :=
assume h1 : ∃ x, A x ∧ B x,
exists.elim h1 $
assume y (h2 : A y ∧ B y),
have h3 : A y, from and.left h2,
show ∃ x, A x, from exists.intro y h3
下面的例子更加复杂:
The following example is more involved:
example : (∃ x, A x ∨ B x) → (∃ x, A x) ∨ (∃ x, B x) :=
assume h1 : ∃ x, A x ∨ B x,
exists.elim h1 $
assume y (h2 : A y ∨ B y),
or.elim h2
(assume h3 : A y,
have h4 : ∃ x, A x, from exists.intro y h3,
show (∃ x, A x) ∨ (∃ x, B x), from or.inl h4)
(assume h3 : B y,
have h4 : ∃ x, B x, from exists.intro y h3,
show (∃ x, A x) ∨ (∃ x, B x), from or.inr h4)
example : (∃ x, A x ∨ B x) → (∃ x, A x) ∨ (∃ x, B x) :=
assume h1 : ∃ x, A x ∨ B x,
exists.elim h1 $
assume y (h2 : A y ∨ B y),
or.elim h2
(assume h3 : A y,
have h4 : ∃ x, A x, from exists.intro y h3,
show (∃ x, A x) ∨ (∃ x, B x), from or.inl h4)
(assume h3 : B y,
have h4 : ∃ x, B x, from exists.intro y h3,
show (∃ x, A x) ∨ (∃ x, B x), from or.inr h4)
再次注意语句中括号的位置。
Note again the placement of parentheses in the statement.
在上一章中,我们考虑了如下自然演绎证明:
In the last chapter, we considered the following natural deduction proof:
以下是精益中相同含义的证明:
Here is a proof of the same implication in Lean:
variable U : Type
variables A B : U → Prop
example : (∀ x, A x → ¬ B x) → ¬ ∃ x, A x ∧ B x :=
assume h1 : ∀ x, A x → ¬ B x,
assume h2 : ∃ x, A x ∧ B x,
exists.elim h2 $
assume x (h3 : A x ∧ B x),
have h4 : A x, from and.left h3,
have h5 : B x, from and.right h3,
have h6 : ¬ B x, from h1 x h4,
show false, from h6 h5
variable U : Type
variables A B : U → Prop
example : (∀ x, A x → ¬ B x) → ¬ ∃ x, A x ∧ B x :=
assume h1 : ∀ x, A x → ¬ B x,
assume h2 : ∃ x, A x ∧ B x,
exists.elim h2 $
assume x (h3 : A x ∧ B x),
have h4 : A x, from and.left h3,
have h5 : B x, from and.right h3,
have h6 : ¬ B x, from h1 x h4,
show false, from h6 h5
这里,我们使用exists.elim来引入一个x满足 的值。名称是任意的;我们也可以使用:A x ∧ B xz
Here, we use exists.elim to introduce a value x satisfying A x ∧ B x. The name is arbitrary; we could just as well have used z:
example : (∀ x, A x → ¬ B x) → ¬ ∃ x, A x ∧ B x :=
assume h1 : ∀ x, A x → ¬ B x,
assume h2 : ∃ x, A x ∧ B x,
exists.elim h2 $
assume z (h3 : A z ∧ B z),
have h4 : A z, from and.left h3,
have h5 : B z, from and.right h3,
have h6 : ¬ B z, from h1 z h4,
show false, from h6 h5
example : (∀ x, A x → ¬ B x) → ¬ ∃ x, A x ∧ B x :=
assume h1 : ∀ x, A x → ¬ B x,
assume h2 : ∃ x, A x ∧ B x,
exists.elim h2 $
assume z (h3 : A z ∧ B z),
have h4 : A z, from and.left h3,
have h5 : B z, from and.right h3,
have h6 : ¬ B z, from h1 z h4,
show false, from h6 h5
以下是存在消除规则的另一个示例:
Here is another example of the exists-elimination rule:
variable U : Type
variable u : U
variable P : Prop
example : (∃x : U, P) ↔ P :=
iff.intro
(assume h1 : ∃x, P,
exists.elim h1 $
assume x (h2 : P),
h2)
(assume h1 : P,
exists.intro u h1)
variable U : Type
variable u : U
variable P : Prop
example : (∃x : U, P) ↔ P :=
iff.intro
(assume h1 : ∃x, P,
exists.elim h1 $
assume x (h2 : P),
h2)
(assume h1 : P,
exists.intro u h1)
u这很微妙:如果我们不声明类型的变量U,即使没有出现在定理的陈述中,证明也不会通过u。这突出了一阶逻辑和 Lean 中实现的逻辑之间的区别。在自然演绎中,我们可以证明(∀ x : U, P x) → ∃ x : U, P xuU
This is subtle: the proof does not go through if we do not declare a variable u of type U, even though u does not appear in the statement of the theorem. This highlights a difference between first-order logic and the logic implemented in Lean. In natural deduction, we can prove (∀ x : U, P x) → ∃ x : U, P x is not provable in Lean. In other words, in Lean, it is possible for a type to be empty, and so the proof above requires an explicit assumption that there is an element u in U.
9.4.等式和计算证明¶
9.4. Equality and calculational proofs¶
在精益中,反身性、对称性和传递性分别称为eq.refl、eq.symm和eq.trans,而第二条替换规则称为eq.subst。它们的用途如下所示。
In Lean, reflexivity, symmetry, and transitivity are called eq.refl, eq.symm, and eq.trans, and the second substitution rule is called eq.subst. Their uses are illustrated below.
variable A : Type
variables x y z : A
variable P : A → Prop
example : x = x :=
show x = x, from eq.refl x
example : y = x :=
have h : x = y, from sorry,
show y = x, from eq.symm h
example : x = z :=
have h1 : x = y, from sorry,
have h2 : y = z, from sorry,
show x = z, from eq.trans h1 h2
example : P y :=
have h1 : x = y, from sorry,
have h2 : P x, from sorry,
show P y, from eq.subst h1 h2
variable A : Type
variables x y z : A
variable P : A → Prop
example : x = x :=
show x = x, from eq.refl x
example : y = x :=
have h : x = y, from sorry,
show y = x, from eq.symm h
example : x = z :=
have h1 : x = y, from sorry,
have h2 : y = z, from sorry,
show x = z, from eq.trans h1 h2
example : P y :=
have h1 : x = y, from sorry,
have h2 : P x, from sorry,
show P y, from eq.subst h1 h2
上述规则eq.refl假设x为论据,因为没有假设可以推断它。所有其他规则都假设其前提为论据。以下是等式推理的一个例子:
The rule eq.refl above assumes x as an argument, because there is no hypothesis to infer it from. All the other rules assume their premises as arguments. Here is an example of equational reasoning:
variables (A : Type) (x y z : A)
example : y = x → y = z → x = z :=
assume h1 : y = x,
assume h2 : y = z,
have h3 : x = y, from eq.symm h1,
show x = z, from eq.trans h3 h2
variables (A : Type) (x y z : A)
example : y = x → y = z → x = z :=
assume h1 : y = x,
assume h2 : y = z,
have h3 : x = y, from eq.symm h1,
show x = z, from eq.trans h3 h2
这个证明可以写得更简洁一些:
This proof can be written more concisely:
example : y = x → y = z → x = z :=
assume h1 h2, eq.trans (eq.symm h1) h2
example : y = x → y = z → x = z :=
assume h1 h2, eq.trans (eq.symm h1) h2
然而,因为计算在数学中非常重要,所以 Lean 提供了更有效的方法来实现计算。一种方法是使用策略。在任何需要证明的地方rewrite键入begin和都会将 Lean 置于策略模式,这提供了一种编写证明的另一种方法:不是直接编写证明,而是向 Lean 提供一系列指令,这些指令向 Lean 展示如何构建相关陈述的证明。要证明的陈述称为目标,许多指令通过将目标转换为更容易证明的内容来取得进展。对目标进行替换的命令就是一个很好的例子。前面的例子可以按如下方式证明:endrewrite
Because calculations are so important in mathematics, however, Lean provides more efficient ways of carrying them out. One method is to use the rewrite tactic. Typing begin and end anywhere a proof is expected puts Lean into tactic mode, which provides an alternative way of writing a proof: rather than writing it directly, you provide Lean with a list of instructions that show Lean how to construct a proof of the statement in question. The statement to be proved is called the goal, and many instructions make progress by transforming the goal into something that is easier to prove. The rewrite command, which carries out a substitution on the goal, is a good example. The previous example can be proved as follows:
example : y = x → y = z → x = z :=
assume h1 : y = x,
assume h2 : y = z,
show x = z,
begin
rewrite ←h1,
apply h2
end
example : y = x → y = z → x = z :=
assume h1 : y = x,
assume h2 : y = z,
show x = z,
begin
rewrite ←h1,
apply h2
end
如果将光标放在单词 之后begin,Lean 会告诉您此时的目标是证明。第一个命令将目标更改为;之前的左箭头(您可以输入)告诉 Lean 反向使用方程式。如果将光标放在逗号之后,Lean 会向您显示新目标 。该命令用于完成证明。x = zx = zy = zh1\<-y = zapplyh2
If you put the cursor after the word begin, Lean will tell you that the goal at that point is to prove x = z. The first command changes the goal x = z to y = z; the left-facing arrow before h1 (which you can enter as \<-) tells Lean to use the equation in the reverse direction. If you put the cursor after the comma, Lean shows you the new goal, y = z. The apply command uses h2 to complete the proof.
h1另一种方法是使用和重写目标h2,从而将目标简化为。发生这种情况时,会自动应用反身性。重写是精益中非常常见的操作,因此我们可以使用简写代替完整的。x = xrewriterwrewrite
An alternative is to rewrite the goal using h1 and h2, which reduces the goal to x = x. When that happens, rewrite automatically applies reflexivity. Rewriting is such a common operation in Lean that we can use the shorthand rw in place of the full rewrite.
example : y = x → y = z → x = z :=
assume h1 : y = x,
assume h2 : y = z,
show x = z,
begin
rw ←h1,
rw h2
end
example : y = x → y = z → x = z :=
assume h1 : y = x,
assume h2 : y = z,
show x = z,
begin
rw ←h1,
rw h2
end
事实上,可以使用方括号组合一系列重写:
In fact, a sequence of rewrites can be combined, using square brackets:
example : y = x → y = z → x = z :=
assume h1 : y = x,
assume h2 : y = z,
show x = z,
begin
rw [←h1, h2]
end
example : y = x → y = z → x = z :=
assume h1 : y = x,
assume h2 : y = z,
show x = z,
begin
rw [←h1, h2]
end
当您将证明简化为单一策略时,您可以使用by而不是。begin ... end
And when you reduce a proof to a single tactic, you can use by instead of begin ... end.
example : y = x → y = z → x = z :=
assume h1 : y = x,
assume h2 : y = z,
show x = z, by rw [←h1, h2]
example : y = x → y = z → x = z :=
assume h1 : y = x,
assume h2 : y = z,
show x = z, by rw [←h1, h2]
如果您将光标放在 之后←h1,Lean 会显示该点的目标。
If you put the cursor after the ←h1, Lean shows you the goal at that point.
在接下来的章节中我们将看到,在普通的数学证明中,人们通常以这样的格式进行计算:
We will see in the coming chapters that in ordinary mathematical proofs, one commonly carries out calculations in a format like this:
Lean 有一种机制来模拟此类计算证明。每当需要证明某个等式时,您都可以使用标识符 提供证明calc,然后跟上一系列等式和证明,形式如下:
Lean has a mechanism to model such calculational proofs. Whenever a proof of an equation is expected, you can provide a proof using the identifier calc, following by a chain of equalities and justification, in the following form:
calc
e1 = e2 : justification 1
... = e3 : justification 2
... = e4 : justification 3
... = e5 : justification 4
calc
e1 = e2 : justification 1
... = e3 : justification 2
... = e4 : justification 3
... = e5 : justification 4
这个链条可以一直延伸到需要的长度,在这个例子中,结果是 的证明。每个证明都是所用假设或定理的名称。例如,前面的证明可以写成如下形式:e1 = e5
The chain can go on as long as needed, and in this example the result is a proof of e1 = e5. Each justification is the name of the assumption or theorem that is used. For example, the previous proof could be written as follows:
example : y = x → y = z → x = z :=
assume h1 : y = x,
assume h2 : y = z,
calc
x = y : eq.symm h1
... = z : h2
example : y = x → y = z → x = z :=
assume h1 : y = x,
assume h2 : y = z,
calc
x = y : eq.symm h1
... = z : h2
像往常一样,语法很挑剔;请注意,表达式中没有逗号calc,冒号和点需要以该形式准确输入。唯一不同的是表达式和理由本身。e1, e2, e3, ...
As usual, the syntax is finicky; notice that there are no commas in the calc expression, and the colons and dots need to be entered exactly in that form. All that varies are the expressions e1, e2, e3, ... and the justifications themselves.
环境calc与 结合使用时功能最强大rewrite,因为我们可以使用库中的事实重写表达式。例如,Lean 的库有许多整数的基本恒等式,例如:
The calc environment is most powerful when used in conjunction with rewrite, since we can then rewrite expressions with facts from the library. For example, Lean’s library has a number of basic identities for the integers, such as these:
import data.int.basic
variables x y z : int
example : x + 0 = x :=
add_zero x
example : 0 + x = x :=
zero_add x
example : (x + y) + z = x + (y + z) :=
add_assoc x y z
example : x + y = y + x :=
add_comm x y
example : (x * y) * z = x * (y * z) :=
mul_assoc x y z
example : x * y = y * x :=
mul_comm x y
example : x * (y + z) = x * y + x * z :=
left_distrib x y z
example : (x + y) * z = x * z + y * z :=
right_distrib x y z
import data.int.basic
variables x y z : int
example : x + 0 = x :=
add_zero x
example : 0 + x = x :=
zero_add x
example : (x + y) + z = x + (y + z) :=
add_assoc x y z
example : x + y = y + x :=
add_comm x y
example : (x * y) * z = x * (y * z) :=
mul_assoc x y z
example : x * y = y * x :=
mul_comm x y
example : x * (y + z) = x * y + x * z :=
left_distrib x y z
example : (x + y) * z = x * z + y * z :=
right_distrib x y z
您还可以将整数类型写为ℤ,使用\Z或输入\int我们已经导入了文件data.int.basic,以便我们可以使用整数的所有基本属性。 (在后面的代码片段中,我们将在在线和 pdf 版本的教科书中删除此行,以避免混乱。)例如,请注意add_comm是定理。因此,要将其实例化为,您可以写。使用这些公理,以下是在 Lean 中呈现的上述计算,作为关于整数的定理:∀ x y, x + y = y + xs + t = t + sadd_comm s t
You can also write the type of integers as ℤ, entered with either \Z or \int We have imported the file data.int.basic to make all the basic properties of the integers available to us. (In later snippets, we will suppress this line in the online and pdf versions of the textbook, to avoid clutter.) Notice that, for example, add_comm is the theorem ∀ x y, x + y = y + x. So to instantiate it to s + t = t + s, you write add_comm s t. Using these axioms, here is the calculation above rendered in Lean, as a theorem about the integers:
example (x y z : int) : (x + y) + z = (x + z) + y :=
calc
(x + y) + z = x + (y + z) : add_assoc x y z
... = x + (z + y) : eq.subst (add_comm y z) rfl
... = (x + z) + y : eq.symm (add_assoc x z y)
example (x y z : int) : (x + y) + z = (x + z) + y :=
calc
(x + y) + z = x + (y + z) : add_assoc x y z
... = x + (z + y) : eq.subst (add_comm y z) rfl
... = (x + z) + y : eq.symm (add_assoc x z y)
使用rewrite更有效,但有时我们必须提供信息来指定规则的使用位置:
Using rewrite is more efficient, though at times we have to provide information to specify where the rules are used:
example (x y z : int) : (x + y) + z = (x + z) + y :=
calc
(x + y) + z = x + (y + z) : by rw add_assoc
... = x + (z + y) : by rw [add_comm y z]
... = (x + z) + y : by rw add_assoc
example (x y z : int) : (x + y) + z = (x + z) + y :=
calc
(x + y) + z = x + (y + z) : by rw add_assoc
... = x + (z + y) : by rw [add_comm y z]
... = (x + z) + y : by rw add_assoc
在这种情况下,我们可以使用一个rewrite:
In that case, we can use a single rewrite:
example (x y z : int) : (x + y) + z = (x + z) + y :=
by rw [add_assoc, add_comm y z, add_assoc]
example (x y z : int) : (x + y) + z = (x + z) + y :=
by rw [add_assoc, add_comm y z, add_assoc]
如果在序列rewrites充分之前#check证明,则错误消息将显示剩余的目标。
If you #check the proof before the sequence of rewrites is sufficient, the error message will display the remaining goal.
下面是另一个示例:
Here is another example:
variables a b d c : int
example : (a + b) * (c + d) = a * c + b * c + a * d + b * d :=
calc
(a + b) * (c + d) = (a + b) * c + (a + b) * d : by rw left_distrib
... = (a * c + b * c) + (a + b) * d : by rw right_distrib
... = (a * c + b * c) + (a * d + b * d) : by rw right_distrib
... = a * c + b * c + a * d + b * d : by rw ←add_assoc
variables a b d c : int
example : (a + b) * (c + d) = a * c + b * c + a * d + b * d :=
calc
(a + b) * (c + d) = (a + b) * c + (a + b) * d : by rw left_distrib
... = (a * c + b * c) + (a + b) * d : by rw right_distrib
... = (a * c + b * c) + (a * d + b * d) : by rw right_distrib
... = a * c + b * c + a * d + b * d : by rw ←add_assoc
再次,我们可以得到一个更短的证明:
Once again, we can get by with a shorter proof:
example : (a + b) * (c + d) = a * c + b * c + a * d + b * d :=
by rw [left_distrib, right_distrib, right_distrib, ←add_assoc]
example : (a + b) * (c + d) = a * c + b * c + a * d + b * d :=
by rw [left_distrib, right_distrib, right_distrib, ←add_assoc]
9.5.练习¶
9.5. Exercises¶
填写
sorry。section variable A : Type variable f : A → A variable P : A → Prop variable h : ∀ x, P x → P (f x) -- Show the following: example : ∀ y, P y → P (f (f y)) := sorry end
Fill in the
sorry.section variable A : Type variable f : A → A variable P : A → Prop variable h : ∀ x, P x → P (f x) -- Show the following: example : ∀ y, P y → P (f (f y)) := sorry end
填写
sorry。section variable U : Type variables A B : U → Prop example : (∀ x, A x ∧ B x) → ∀ x, A x := sorry end
Fill in the
sorry.section variable U : Type variables A B : U → Prop example : (∀ x, A x ∧ B x) → ∀ x, A x := sorry end
填写
sorry。section variable U : Type variables A B C : U → Prop variable h1 : ∀ x, A x ∨ B x variable h2 : ∀ x, A x → C x variable h3 : ∀ x, B x → C x example : ∀ x, C x := sorry end
Fill in the
sorry.section variable U : Type variables A B C : U → Prop variable h1 : ∀ x, A x ∨ B x variable h2 : ∀ x, A x → C x variable h3 : ∀ x, B x → C x example : ∀ x, C x := sorry end
填写
sorry下面的,以证明理发师悖论。open classical -- not needed, but you can use it -- This is an exercise from Chapter 4. Use it as an axiom here. axiom not_iff_not_self (P : Prop) : ¬ (P ↔ ¬ P) example (Q : Prop) : ¬ (Q ↔ ¬ Q) := not_iff_not_self Q section variable Person : Type variable shaves : Person → Person → Prop variable barber : Person variable h : ∀ x, shaves barber x ↔ ¬ shaves x x -- Show the following: example : false := sorry end
Fill in the
sorry’s below, to prove the barber paradox.open classical -- not needed, but you can use it -- This is an exercise from Chapter 4. Use it as an axiom here. axiom not_iff_not_self (P : Prop) : ¬ (P ↔ ¬ P) example (Q : Prop) : ¬ (Q ↔ ¬ Q) := not_iff_not_self Q section variable Person : Type variable shaves : Person → Person → Prop variable barber : Person variable h : ∀ x, shaves barber x ↔ ¬ shaves x x -- Show the following: example : false := sorry end
填写
sorry。section variable U : Type variables A B : U → Prop example : (∃ x, A x) → ∃ x, A x ∨ B x := sorry end
Fill in the
sorry.section variable U : Type variables A B : U → Prop example : (∃ x, A x) → ∃ x, A x ∨ B x := sorry end
填写
sorry。section variable U : Type variables A B : U → Prop variable h1 : ∀ x, A x → B x variable h2 : ∃ x, A x example : ∃ x, B x := sorry end
Fill in the
sorry.section variable U : Type variables A B : U → Prop variable h1 : ∀ x, A x → B x variable h2 : ∃ x, A x example : ∃ x, B x := sorry end
填写
sorry。variable U : Type variables A B C : U → Prop example (h1 : ∃ x, A x ∧ B x) (h2 : ∀ x, B x → C x) : ∃ x, A x ∧ C x := sorry
Fill in the
sorry.variable U : Type variables A B C : U → Prop example (h1 : ∃ x, A x ∧ B x) (h2 : ∀ x, B x → C x) : ∃ x, A x ∧ C x := sorry
完成这些证明。
variable U : Type variables A B C : U → Prop example : (¬ ∃ x, A x) → ∀ x, ¬ A x := sorry example : (∀ x, ¬ A x) → ¬ ∃ x, A x := sorry
Complete these proofs.
variable U : Type variables A B C : U → Prop example : (¬ ∃ x, A x) → ∀ x, ¬ A x := sorry example : (∀ x, ¬ A x) → ¬ ∃ x, A x := sorry
填写
sorry。variable U : Type variables R : U → U → Prop example : (∃ x, ∀ y, R x y) → ∀ y, ∃ x, R x y := sorry
Fill in the
sorry.variable U : Type variables R : U → U → Prop example : (∃ x, ∀ y, R x y) → ∀ y, ∃ x, R x y := sorry
下面的练习表明,在存在反身性的情况下,对称性和传递性规则等同于单一规则。
theorem foo {A : Type} {a b c : A} : a = b → c = b → a = c := sorry -- notice that you can now use foo as a rule. The curly braces mean that -- you do not have to give A, a, b, or c section variable A : Type variables a b c : A example (h1 : a = b) (h2 : c = b) : a = c := foo h1 h2 end section variable {A : Type} variables {a b c : A} -- replace the sorry with a proof, using foo and rfl, without using eq.symm. theorem my_symm (h : b = a) : a = b := sorry -- now use foo and my_symm to prove transitivity theorem my_trans (h1 : a = b) (h2 : b = c) : a = c := sorry end
The following exercise shows that in the presence of reflexivity, the rules for symmetry and transitivity are equivalent to a single rule.
theorem foo {A : Type} {a b c : A} : a = b → c = b → a = c := sorry -- notice that you can now use foo as a rule. The curly braces mean that -- you do not have to give A, a, b, or c section variable A : Type variables a b c : A example (h1 : a = b) (h2 : c = b) : a = c := foo h1 h2 end section variable {A : Type} variables {a b c : A} -- replace the sorry with a proof, using foo and rfl, without using eq.symm. theorem my_symm (h : b = a) : a = b := sorry -- now use foo and my_symm to prove transitivity theorem my_trans (h1 : a = b) (h2 : b = c) : a = c := sorry end
sorry用列表中的正确公理替换下面的每一个。import data.int.basic -- these are the axioms for a commutative ring #check @add_assoc #check @add_comm #check @add_zero #check @zero_add #check @mul_assoc #check @mul_comm #check @mul_one #check @one_mul #check @left_distrib #check @right_distrib #check @add_left_neg #check @add_right_neg #check @sub_eq_add_neg variables x y z : int theorem t1 : x - x = 0 := calc x - x = x + -x : by rw sub_eq_add_neg ... = 0 : by rw add_right_neg theorem t2 (h : x + y = x + z) : y = z := calc y = 0 + y : by rw zero_add ... = (-x + x) + y : by rw add_left_neg ... = -x + (x + y) : by rw add_assoc ... = -x + (x + z) : by rw h ... = (-x + x) + z : by rw add_assoc ... = 0 + z : by rw add_left_neg ... = z : by rw zero_add theorem t3 (h : x + y = z + y) : x = z := calc x = x + 0 : sorry ... = x + (y + -y) : sorry ... = (x + y) + -y : sorry ... = (z + y) + -y : by rw h ... = z + (y + -y) : sorry ... = z + 0 : sorry ... = z : sorry theorem t4 (h : x + y = 0) : x = -y := calc x = x + 0 : by rw add_zero ... = x + (y + -y) : by rw add_right_neg ... = (x + y) + -y : by rw add_assoc ... = 0 + -y : by rw h ... = -y : by rw zero_add theorem t5 : x * 0 = 0 := have h1 : x * 0 + x * 0 = x * 0 + 0, from calc x * 0 + x * 0 = x * (0 + 0) : sorry ... = x * 0 : sorry ... = x * 0 + 0 : sorry, show x * 0 = 0, from t2 _ _ _ h1 theorem t6 : x * (-y) = -(x * y) := have h1 : x * (-y) + x * y = 0, from calc x * (-y) + x * y = x * (-y + y) : sorry ... = x * 0 : sorry ... = 0 : by rw t5 x, show x * (-y) = -(x * y), from t4 _ _ h1 theorem t7 : x + x = 2 * x := calc x + x = 1 * x + 1 * x : by rw one_mul ... = (1 + 1) * x : sorry ... = 2 * x : rfl
Replace each
sorrybelow by the correct axiom from the list.import data.int.basic -- these are the axioms for a commutative ring #check @add_assoc #check @add_comm #check @add_zero #check @zero_add #check @mul_assoc #check @mul_comm #check @mul_one #check @one_mul #check @left_distrib #check @right_distrib #check @add_left_neg #check @add_right_neg #check @sub_eq_add_neg variables x y z : int theorem t1 : x - x = 0 := calc x - x = x + -x : by rw sub_eq_add_neg ... = 0 : by rw add_right_neg theorem t2 (h : x + y = x + z) : y = z := calc y = 0 + y : by rw zero_add ... = (-x + x) + y : by rw add_left_neg ... = -x + (x + y) : by rw add_assoc ... = -x + (x + z) : by rw h ... = (-x + x) + z : by rw add_assoc ... = 0 + z : by rw add_left_neg ... = z : by rw zero_add theorem t3 (h : x + y = z + y) : x = z := calc x = x + 0 : sorry ... = x + (y + -y) : sorry ... = (x + y) + -y : sorry ... = (z + y) + -y : by rw h ... = z + (y + -y) : sorry ... = z + 0 : sorry ... = z : sorry theorem t4 (h : x + y = 0) : x = -y := calc x = x + 0 : by rw add_zero ... = x + (y + -y) : by rw add_right_neg ... = (x + y) + -y : by rw add_assoc ... = 0 + -y : by rw h ... = -y : by rw zero_add theorem t5 : x * 0 = 0 := have h1 : x * 0 + x * 0 = x * 0 + 0, from calc x * 0 + x * 0 = x * (0 + 0) : sorry ... = x * 0 : sorry ... = x * 0 + 0 : sorry, show x * 0 = 0, from t2 _ _ _ h1 theorem t6 : x * (-y) = -(x * y) := have h1 : x * (-y) + x * y = 0, from calc x * (-y) + x * y = x * (-y + y) : sorry ... = x * 0 : sorry ... = 0 : by rw t5 x, show x * (-y) = -(x * y), from t4 _ _ h1 theorem t7 : x + x = 2 * x := calc x + x = 1 * x + 1 * x : by rw one_mul ... = (1 + 1) * x : sorry ... = 2 * x : rfl
10.一阶逻辑的语义¶
10. Semantics of First Order Logic¶
在第 6 章中,我们强调了命题逻辑的语法和语义之间的区别。语法问题与公式的形式结构以及可以得出不同类型公式的条件有关。另一方面,语义问题涉及公式相对于某些真值赋值的真值。
In Chapter 6, we emphasized a distinction between the syntax and the semantics of propositional logic. Syntactic questions have to do with the formal structure of formulas and the conditions under which different types of formulas can be derived. Semantic questions, on the other hand, concern the truth of a formula relative to some truth assignment.
正如你所料,我们可以在一阶逻辑的设置中做出类似的区分。前两章重点介绍了语法,但也引入了一些语义思想。回想一下感兴趣的领域
As you might expect, we can make a similar distinction in the setting of first order logic. The previous two chapters have focused on syntax, but some semantic ideas have slipped in. Recall the running example with domain of interest
这表明,一阶句子的真假取决于我们如何解释量词和语言的基本关系。但有些公式在任何解释下都是正确的:例如,
This indicates that the truth or falsity or a first order sentence can depend on how we interpret the quantifiers and basic relations of the language. But some formulas are true under any interpretation: for instance,
我们可以扩大类比范围:一阶逻辑中的“模型”类似于命题逻辑中的真值分配。在命题情况下,选择真值分配使我们能够为语言的所有公式分配真值;现在,选择模型将使我们能够为一阶语言的所有句子分配真值。下一节的目的是使这个概念更加精确。
We can broaden the analogy: a “model” in first order logic is the analogue of a truth assignment in propositional logic. In the propositional case, choosing a truth assignment allowed us to assign truth values to all formulas of the language; now, choosing a model will allow us to assign truth values to all sentences of a first order language. The aim of the next section is to make this notion more precise.
10.1.解释¶
10.1. Interpretations¶
在我们的示例中,语言的符号是 0、1、
The symbols of the language in our running example—0, 1,
答案当然是,我们没有足够的信息来说明。谓词没有明显的含义
The answer, of course, is that we don’t have enough information to say. There’s no obvious meaning to the predicates
我们将这些描述称为谓词符号的解释
We call each of these descriptions an interpretation of the predicate symbols
我们可以以类似的方式解释常数、函数和关系符号。常数符号的解释
We can interpret constant, function, and relation symbols in a similar way. An interpretation of constant symbol
强调句法谓词符号(或函数符号、常量符号)与它所解释的语义谓词(或函数、对象)之间的区别非常重要。前者是一个符号,与其他符号相关,在我们指定解释之前,它本身没有任何意义。严格来说,写成
It is important to emphasize the difference between a syntactic predicate symbol (or function symbol, or constant symbol) and the semantic predicate (or function, or object) to which it is interpreted. The former is a symbol, relates to other symbols, and has no meaning on its own until we specify an interpretation. Strictly speaking, it makes no sense to write
有时,当我们在特定领域解释一种语言时,隐式地在语言中引入新的常量符号来表示该领域的元素是很有用的。具体来说,对于每个元素
Sometimes, when we interpret a language in a particular domain, it is useful to implicitly introduce new constant symbols into the language to denote elements of this domain. Specifically, for each element
10.2.模型中的真相¶
10.2. Truth in a Model¶
修复一阶语言。假设我们选择了一个域
Fix a first-order language. Suppose we have chosen a domain
基于模型评估句子真实性的过程正如你所想的那样,但正式的描述却很微妙。回想一下我们在第 4 章中提到的术语和断言之间的区别。术语,如
The procedure for evaluating the truth of a sentence based on a model works the way you think it should, but the formal description is subtle. Recall the difference between terms and assertions that we made earlier in Chapter 4. Terms, like
模型中术语的解释是该模型域的一个元素。模型直接指定如何解释常量符号。解释术语
The interpretation of a term in a model is an element of the domain of that model. The model directly specifies how to interpret constant symbols. To interpret a term
例如,假设我们有一种具有两个常量符号的语言,
For example, suppose we have a language with two constant symbols,
类似地,断言的解释是一个值
Similarly, the interpretation of an assertion is a value
要解释应用于某些术语的谓词或关系,我们首先将这些术语解释为域中的对象,然后查看关系符号的解释是否适用于这些对象。继续这个例子,假设我们的语言也有一个关系符号
To interpret a predicate or relation applied to some terms, we first interpret the terms as objects in the domain, and then see if the interpretation of the relation symbol is true of those objects. To continue with the example, suppose our language also has a relation symbol
使用逻辑连接词解释表达式
Interpreting expressions using the logical connectives
我们仍然需要解释如何解释存在性和普遍性表达式。我们看到
We still need to explain how to interpret existential and universal expressions. We saw that
更简洁地说,我们可以说
More concisely, we can say that
最后,请记住,
Finally, remember,
这些规则使我们能够确定模型中任何句子的真值。(请记住,句子是没有自由变量的公式。)有一些微妙之处:例如,我们隐式地假设我们的公式不会对同一变量进行两次量化,就像
These rules allow us to determine the truth value of any sentence in a model. (Remember, a sentence is a formula with no free variables.) There are some subtleties: for instance, we’ve implicitly assumed that our formula doesn’t quantify over the same variable twice, as in
10.3.示例¶
10.3. Examples¶
采用一种没有常量符号、只有一个关系符号的简单语言
Take a simple language with no constant symbols, one relation symbol
在阅读以下答案之前,请先思考以下问题。请记住,我们的域名是
Think about the following questions before you read the answers below. Remember, our domain is
是真的吗
? ?Is it true that
? What about ?同样地,
? ?Similarly, what about
? ?做句子
和 意思相同吗?它们是真的还是假的?Do the sentences
and mean the same thing? Are they true or false?你能想到一个公式吗
在这个语言中,有一个自由变量 ,这样 但 ?Can you think of a formula
in this language, with one free variable , such that but ?
这些问题表明了全称量词和存在量词之间存在微妙且通常很棘手的相互作用。一旦你对它们进行了一番思考,请阅读以下答案:
These questions indicate a subtle, and often tricky, interplay between the universal and existential quantifiers. Once you’ve thought about them a bit, read the answers:
这两个陈述都是正确的。对于前者,我们可以(例如)解释
作为自然数 0。那么, ,所以存在性为真。对于后者,选择任意自然数 ;情况仍然是这样的,当我们解释 作为 ,我们有 。Both of these statements are true. For the former, we can (for example) interpret
as the natural number 0. Then, , so the existential is true. For the latter, pick an arbitrary natural number ; it is still the case that when we interpret as , we have .第一个陈述是正确的,因为我们可以解释
为 0。但第二个陈述是错误的。当我们解释 为 1(或者,事实上,为 0 以外的任何自然数),我们看到 。The first statement is true, since we can interpret
as 0. The second statement, though, is false. When we interpret as 1 (or, in fact, as any natural number besides 0), we see that .这些句子的意思并不相同,尽管在指定的模型中,它们都是正确的。第一个句子表示某个自然数小于或等于每个自然数。这是正确的:0 小于或等于每个自然数。第二个句子说,对于每个自然数,都有另一个至少同样大的自然数。同样,这是正确的:每个自然数
小于或等于 。如果我们将我们的域名设为 而不是 ,第一句话就是假的,而第二句话仍然是正确的。These sentences do not mean the same thing, although in the specified model, both are true. The first expresses that some natural number is less than or equal to every natural number. This is true: 0 is less than or equal to every natural number. The second sentence says that for every natural number, there is another natural number at least as big. Again, this is true: every natural number
is less than or equal to . If we took our domain to be instead of , the first sentence would be false, while the second would still be true.这里描述的情况在我们的模型中是不可能发生的。如果
, 然后 ,这意味着 。这种情况唯一可能发生的时候就是当我们的模型域为空的时候。The situation described here is impossible in our model. If
, then , which implies that . The only time this situation can happen is when the domain of our model is empty.
现在考虑一种具有常量符号 2 的不同语言,谓词符号
Now consider a different language with constant symbol 2, predicate symbols
对于下一个示例,我们将考虑由“点”的矩形网格给出的模型。每个点都有颜色(红色、蓝色或绿色)和大小(小或大)。我们使用字母
For the next example, we will consider models that are given by a rectangular grid of “dots.” Each dot has a color (red, blue, or green) and a size (small or large). We use the letter
我们用来描述点世界的逻辑语言有谓词
The logical language we use to describe our dot world has predicates
考虑以下句子:
Consider the following sentences:
我们可以在以下特定模型中对它们进行评估:
We can evaluate them in this particular model:
R R |
r r |
克 g |
b b |
R R |
b b |
格 G |
b b |
乙 B |
乙 B |
乙 B |
b b |
它们具有以下真值:
There they have the following truth values:
错误的
false
真的
true
真的
true
错误的
false
真的
true
真的
true
错误的
false
真的
true
错误的
false
真的
true
对于每个句子,看看你是否能找到一个模型使句子为真,另一个模型使句子为假。为了增加挑战性,尝试同时使所有句子为真。请注意,你可以使用任意数量的行和任意数量的列。
For each sentence, see if you can find a model that makes the sentence true, and another that makes it false. For an extra challenge, try to make all of the sentences true simultaneously. Notice that you can use any number of rows and any number of columns.
10.4.有效性和逻辑结果¶
10.4. Validity and Logical Consequence¶
我们已经看到,公式的真假往往取决于我们选择的模型。然而,有些公式在每个可能的模型中都是正确的。我们之前看到的一个例子是
We have seen that whether a formula is true or false often depends on the model we choose. Some formulas, though, are true in every possible model. An example we saw earlier was
在命题设置中,有一种简单的方法可以确定公式是否是重言式。编写真值表并检查是否有任何以结尾的行
In the propositional setting, there is an easy method to figure out if a formula is a tautology or not. Writing the truth table and checking for any rows ending with
这并不是说我们永远无法弄清楚一阶句子是否是重言式。例如,我们曾论证过
This is not to say that we can never figure out if a first-order sentence is a tautology. For example, we have argued that
就像命题逻辑的情况一样,我们可以将有效性的概念扩展为逻辑结果的概念。固定一阶语言,
As was the case with propositional logic, we can extend the notion of validity to a notion of logical consequence. Fix a first-order language,
10.5.健全性和完整性¶
10.5. Soundness and Completeness¶
在命题逻辑中,我们看到了可证明公式和重言式之间的密切联系——具体来说,一个公式是可证明的当且仅当它是重言式。更一般地,我们说一个公式
In propositional logic, we saw a close connection between the provable formulas and the tautologies—specifically, a formula is provable if and only if it is a tautology. More generally, we say that a formula
“健全性”方向——事实上,如果
The “soundness” direction—the fact that if
一阶逻辑的完备性定理最早由库尔特·哥德尔在其 1929 年的论文中证明。后来,莱昂·亨金给出了另一个更简单的证明。
The completeness theorem for first order logic was first proved by Kurt Gödel in his 1929 dissertation. Another, simpler proof was later provided by Leon Henkin.
定理。如果公式
Theorem. If a formula
与命题逻辑版本相比,一阶完备性定理更难证明。我们不会在这里过多地讨论,但会指出一些主要思想。如果不能从这些假设中证明矛盾,则一组句子被称为是一致的。Henkin 证明中的大部分工作都是通过以下“模型存在”定理完成的:
Compared to the version for propositional logic, the first order completeness theorem is harder to prove. We will not go into too much detail here, but will indicate some of the main ideas. A set of sentences is said to be consistent if you cannot prove a contradiction from those hypotheses. Most of the work in Henkin’s proof is done by the following “model existence” theorem:
定理。每一个一致的句子集都有一个模型。
Theorem. Every consistent set of sentences has a model.
从这个定理很容易推导出完备性定理。假设没有证明
From this theorem, it is easy to deduce the completeness theorem. Suppose there is no proof of
模型存在定理的证明非常复杂。人们必须以某种方式从一组一致的句子中构建一个模型。策略是从句法实体构建模型,换句话说,使用扩展语言中的术语作为域的元素。
The proof of the model existence theorem is intricate. Somehow, from a consistent set of sentences, one has to build a model. The strategy is to build the model out of syntactic entities, in other words, to use terms in an expanded language as the elements of the domain.
这里的寓意与命题逻辑的寓意大致相同。因为我们在开发句法规则时已经考虑到了某种语义,所以两者展现了同一枚硬币的不同面:可证明的句子恰好是所有模型中都为真的句子,而从一组假设中可证明的句子恰好是这些假设的所有模型中都为真的句子。
The moral here is much the same as it was for propositional logic. Because we have developed our syntactic rules with a certain semantics in mind, the two exhibit different sides of the same coin: the provable sentences are exactly the ones that are true in all models, and the sentences that are provable from a set of hypotheses are exactly the ones that are true in all models of those hypotheses.
因此,我们有另一种方法来回答上一节提出的问题。要证明一个句子是有效的,没有必要在每个可能的模型中检查它的真实性。相反,只需给出一个证明就足够了。
We therefore have another way to answer the question posed in the previous section. To show that a sentence is valid, there is no need to check its truth in every possible model. Rather, it suffices to produce a proof.
10.6.练习¶
10.6. Exercises¶
在具有二元关系的一阶语言中,
,考虑以下句子:对于下列每个结构,确定每个句子是真还是假。
结构
,即自然数中的解释 是结构
结构
结构
,即自然数中的解释 是“除法”关系结构
,即变量在自然数集上的范围的解释,其中 被解释为子集关系。
In a first-order language with a binary relation,
, consider the following sentences:For each of the following structures, determine whether of each of those sentences is true or false.
the structure
, that is, the interpretation in the natural numbers where isthe structure
the structure
the structure
, that is, the interpretation in the natural numbers where is the “divides” relationthe structure
, that is, the interpretation where variables range over sets of natural numbers, where is interpreted as the subset relation.
创建一个 4 x 4 的“点”世界,使以下所有句子都成立:
Create a 4 x 4 “dots” world that makes all of the following sentences true:
修复一阶语言
,并让 和 是任意两个句子 . 请记住 意味着 是有效的。解开定义,表明如果 , 然后 和 。Fix a first-order language
, and let and be any two sentences in . Remember that means that is valid. Unpacking the definitions, show that if , then and .举一个具体的例子来说明
并不一定意味着 或者 换句话说,选择一种语言 并选择特定的句子 和 使得 有效,但 也不 是有效的。Give a concrete example to show that
does not necessarily imply or . In other words, pick a language and choose particular sentences and such that is valid but neither nor is valid.考虑三个公式
, , 和 .这些句子说 具有反身性、对称性和可训练性。对于每对句子,找到一个模型,使这两个句子为真,第三个句子为假。这表明这些句子在逻辑上是独立的:没有一个句子被其他句子所蕴涵。Consider the three formulas
, , and . These sentences say that is reflexive, symmetric, and trainsitive. For each pair of sentences, find a model that makes those two sentences true and the third false. This shows that these sentences are logically independent: no one is entailed by the others.证明如果一组公式
语义上不一致,那么它必然导致每个公式 . 反之是否也成立?Show that if a set of formulas
is semantically inconsistent, then it entails every formula . Does the converse also hold?给出一个公式
使得集合 是一致的,集合也是一致的 。Give a formula
such that the set is consistent, and so is the set .对于下列每个公式,说明该公式是否有效、可满足或不可满足。
For each the following formulas, show whether the formula is valid, satisfiable, or unsatisfiable.
11.集合¶
11. Sets¶
我们已经到了这本教科书的转折点。从今以后,我们将在很大程度上放弃自然演绎,而专注于普通的数学证明。我们将继续思考如何用符号来表示非正式数学,以及自然演绎规则如何在非正式环境中发挥作用。但重点将放在编写普通数学论证上,而不是设计证明树。精益将继续充当非正式和正式领域之间的桥梁。
We have come to a turning point in this textbook. We will henceforth abandon natural deduction, for the most part, and focus on ordinary mathematical proofs. We will continue to think about how informal mathematics can be represented in symbolic terms, and how the rules of natural deduction play out in the informal setting. But the emphasis will be on writing ordinary mathematical arguments, not designing proof trees. Lean will continue to serve as a bridge between the informal and formal realms.
在本章中,我们将讨论在数学推理中发挥着基础性作用的一个概念,即“集合”。
In this chapter, we consider a notion that has come to play a fundamental role in mathematical reasoning, namely, that of a “set.”
11.1.初等集合论¶
11.1. Elementary Set Theory¶
1895 年,德国数学家格奥尔格·康托 (Georg Cantor) 在《数学年鉴》杂志上发表的一篇文章中,对集合(或用他的术语称为Menge )的概念进行了如下描述:
In a publication in the journal Mathematische Annalen in 1895, the German mathematician Georg Cantor presented the following characterization of the notion of a set (or Menge, in his terminology):
集合是指我们的直觉或思想中的确定的、不同的对象(称为 M 的元素)的任意集合 M,它们共同构成一个整体。
By a set we mean any collection M of determinate, distinct objects (called the elements of M) of our intuition or thought into a whole.
从那时起,集合的概念就被用来统一各种抽象和构造。我们将在后面的章节中讨论公理集合论,它为数学提供了基础,在数学中,一切都可以看作一个集合。
Since then, the notion of a set has been used to unify a wide range of abstractions and constructions. Axiomatic set theory, which we will discuss in a later chapter, provides a foundation for mathematics in which everything can be viewed as a set.
从广义上讲,任何集合都可以是一个集合;例如,我们可以考虑以 Ringo Star(数字 7)为元素的集合,以及以帝国大厦为唯一成员的集合。对于如此广泛的集合概念,我们必须小心:罗素悖论让我们考虑集合
On a broad construal, any collection can be a set; for example, we can consider the set whose elements are Ringo Star, the number 7, and the set whose only member is the Empire State Building. With such a broad notion of set we have to be careful: Russell’s paradox has us consider the set
实际上,数学家在使用集合时并没有那么随心所欲。通常,人们会固定一个域(例如自然数),然后考虑该域的子集。换句话说,我们考虑的是数字集、点集、线集等等,而不是任意的“集合”。在本文中,我们将采用这种惯例:当我们谈论集合时,我们总是隐含地谈论某个域的元素集。
In practice, mathematicians are not so freewheeling in their use of sets. Typically, one fixes a domain such as the natural numbers, and consider subsets of that domain. In other words, we consider sets of numbers, sets of points, sets of lines, and so on, rather than arbitrary “sets.” In this text, we will adopt this convention: when we talk about sets, we are always implicitly talking about sets of elements of some domain.
给定一个集合
Given a set
最后一组写得更简单
This last set is written more simply
使用集合构造符号,我们可以定义许多常见的集合和运算。空集
Using set-builder notation, we can define a number of common sets and operations. The empty set,
对偶地,我们可以定义全集,
Dually, we can define the universal set,
给定两个集合
Given two sets
我们将它们的交集定义为两者的元素集合:
And we define their intersection to be the set of elements of both:
我们定义一组
We define the complement of a set of
我们定义两个集合的集合差
We define the set difference of two sets
如果两个集合有完全相同的元素,则称它们相等。如果
Two sets are said to be equal if they have exactly the same elements. If
还要注意,到目前为止我们所说的关于集合的一切都可以用符号逻辑来表示。我们可以将基本集合和构造函数的定义属性呈现如下:
Notice also that everything we have said about sets so far is readily representable in symbolic logic. We can render the defining properties of the basic sets and constructors as follows:
断言
The assertion that
逻辑学家有时将普通的数学证明描述为非正式的,与自然演绎中的形式化证明相对。在编写非正式证明时,重点是可读性。以下是一个例子。
Logicians sometimes describe ordinary mathematical proofs as informal, in contrast to the formal proofs in natural deduction. When writing informal proofs, the focus is on readability. Here is an example.
定理。设
Theorem. Let
证明。设
Proof. Let
相反,假设
Conversely, suppose
首先假设
First, suppose
第二种情况类似:假设
The second case is similar: suppose
请注意,这个证明看起来一点也不像符号逻辑中的证明。首先,普通的证明倾向于使用文字而不是符号。当然,数学总是使用符号,但不是代替“与”和“非”这样的文字;你很少甚至从未见过这些符号
Notice that this proof does not look anything like a proof in symbolic logic. For one thing, ordinary proofs tend to favor words over symbols. Of course, mathematics uses symbols all the time, but not in place of words like “and” and “not”; you will rarely, if ever, see the symbols
同样,非正式证明的结构是通过普通的段落和标点符号来传达的。不要依赖图形图表、换行符和缩进来传达证明的结构。相反,你应该依赖文学手法,如路标和预示。在深入细节之前,提出证明的大纲或关键思想通常会有所帮助,段落的引言可以帮助引导读者的期望,就像在说明性文章中一样。
Similarly, the structure of an informal proof is conveyed with ordinary paragraphs and punctuation. Don’t rely on pictorial diagrams, line breaks, and indentation to convey the structure of a proof. Rather, you should rely on literary devices like signposting and foreshadowing. It is often helpful to present an outline of a proof or the key ideas before delving into the details, and the introductory sentence of a paragraph can help guide a reader’s expectations, just as it does in an expository essay.
尽管如此,你应该能够在上面的证明中隐含地看到自然演绎的元素。从形式上讲,该定理等同于断言
Nonetheless, you should be able to see elements of natural deduction implicitly in the proof above. In formal terms, the theorem is equivalent to the assertion
证明也同样进行。短语“让
and the proof proceeds accordingly. The phrase “let
以“与”和“或”的形式展开交集和并集的定义,先前证明的“仅当”方向可以用自然演绎来表示,如下:
Modulo the unfolding of definition of intersection and union in terms of “and” and “or,” the “only if” direction of the previous proof could be represented in natural deduction like this:
在下一章中,我们将看到这种逻辑结构在 Lean 中得到体现。但是,用自然演绎法写出长篇证明并不是传达数学思想的最有效方式。因此,我们的目标是教你用自然演绎规则来思考,但用普通英语来表达步骤。
In the next chapter, we will see that this logical structure is made manifest in Lean. But writing long proofs in natural deduction is not the most effective to communicate the mathematical ideas. So our goal here is to teach you to think in terms of natural deduction rules, but express the steps in ordinary English.
这是另一个例子。
Here is another example.
定理。
Theorem.
证明。设
Proof. Let
相反,假设
Conversely, suppose
非正式证明和正式证明之间最大的区别可能在于细节程度。非正式证明通常会跳过那些被认为“直截了当”或“显而易见”的细节,而花更多精力阐明新颖或出乎意料的推论。
Perhaps the biggest difference between informal proofs and formal proofs is the level of detail. Informal proofs will often skip over details that are taken to be “straightforward” or “obvious,” devoting more effort to spelling out inferences that are novel or unexpected.
写出好的证明就像写出好的论文一样。要让读者相信结论是正确的,你必须让他们理解论点,而不要用不必要的细节让他们不知所措。在心中有一个特定的受众会有所帮助。试着向朋友、室友和家人大声说出你的论点;如果他们的眼睛呆滞,那么期望匿名读者做得更好是不合理的。
Writing a good proof is like writing a good essay. To convince your readers that the conclusion is correct, you have to get them to understand the argument, without overwhelming them with unnecessary details. It helps to have a specific audience in mind. Try speaking the argument aloud to friends, roommates, and family members; if their eyes glaze over, it is unreasonable to expect anonymous readers to do better.
学习写好证明的最好方法之一是阅读好的证明,并注意写作风格。挑选一本你认为特别清晰和吸引人的教科书的例子,思考是什么让它如此。
One of the best ways to learn to write good proofs is to read good proofs, and pay attention to the style of writing. Pick an example of a textbook that you find especially clear and engaging, and think about what makes it so.
自然演绎和形式验证可以帮助您理解使证明正确的组成部分,但您必须培养一种直觉,以了解什么使证明易于阅读且令人愉快。
Natural deduction and formal verification can help you understand the components that make a proof correct, but you will have to develop an intuitive feel for what makes a proof easy and enjoyable to read.
11.2.使用集合进行计算¶
11.2. Calculations with Sets¶
计算是数学的核心,数学证明通常涉及执行一系列计算。事实上,计算本身可以看作是两个表达式描述同一实体的证明。
Calculation is a central to mathematics, and mathematical proofs often involve carrying out a sequence of calculations. Indeed, a calculation can be viewed as a proof in and of itself that two expressions describe the same entity.
在高中代数中,学生经常被要求证明如下恒等式:
In high school algebra, students are often asked to prove identities like the following:
主张。
Proposition.
在某些地方,要求学生写这样的证明:
In some places, students are asked to write proofs like this:
证明。
Proof.
数学家们看到这种情况时通常会感到畏缩。不要这样做!这看起来像是正向推理的一个例子,我们从一个复杂的身份开始,最终证明
Mathematicians generally cringe when they see this. Don’t do it! It looks like an instance of forward reasoning, where we start with a complex identity and end up proving
证明。我们想证明
Proof. We want to show
要做到这一点,只需证明
To do that, it suffices to show
为此,只需证明
For that, it suffices to show
但最后一个等式显然是正确的。
But this last equation is clearly true.
然而,这种叙述不太流畅。有时在证明中采用逆向推理是有充分理由的,但在这种情况下,以更直接的方式呈现证明更容易。以下是一个例子:
The narrative doesn’t flow well, however. Sometimes there are good reasons to work backward in a proof, but in this case it is easy to present the proof in a more forward-directed manner. Here is one example:
证明。计算左边,我们有
Proof. Calculating on the left-hand side, we have
在右侧,我们还有
On the right-hand side, we also have
所以
So
在这种情况下,数学家经常使用缩写“LHS”和“RHS”分别表示“左侧”和“右侧”。事实上,在这里我们可以轻松地将证明写成一个前向计算:
Mathematicians often use the abbreviations “LHS” and “RHS” for “left-hand side” and “right-hand side,” respectively, in situations like this. In fact, here we can easily write the proof as a single forward-directed calculation:
证明。
Proof.
这样的证明清晰、紧凑且易于阅读。读者面临的主要挑战是弄清楚每个后续步骤的合理性。数学家有时会用附加信息注释此类计算,或在文本前后添加几句解释。但理想的情况是将计算分成足够小的步骤,以便每个步骤都很简单,不需要解释。(同样,什么才是“简单”取决于阅读证明的人。)
Such a proof is clear, compact, and easy to read. The main challenge to the reader is to figure out what justifies each subsequent step. Mathematicians sometimes annotate such a calculation with additional information, or add a few words of explanation in the text before and/or after. But the ideal situation is to carry out the calculation in small enough steps so that each step is straightforward, and needs no explanation. (And, once again, what counts as “straightforward” will vary depending on who is reading the proof.)
我们说过,如果两个集合具有相同的元素,则它们相等。在上一节中,我们通过推理每个集合的元素来证明两个集合相等,但我们通常可以更高效。假设
We have said that two sets are equal if they have the same elements. In the previous section, we proved that two sets are equal by reasoning about the elements of each, but we can often be more efficient. Assuming
这使我们能够通过计算来证明进一步的身份。以下是一个例子。
This allows us to prove further identities by calculating. Here is an example.
定理。设
Theorem. Let
证明。
Proof.
这是另一个例子。
Here is another example.
定理。设
Theorem. Let
证明。
Proof.
经典地,您可能已经注意到,在逻辑等价性下,命题满足与集合类似的恒等式。这并非巧合;两者都是布尔代数的实例。以下是将上述恒等式翻译成布尔代数语言:
Classically, you may have noticed that propositions, under logical equivalence, satisfy identities similar to sets. That is no coincidence; both are instances of boolean algebras. Here are the identities above translated to the language of a boolean algebra:
翻译成布尔代数的语言,上面的第一定理如下:
Translated to the language of boolean algebras, the first theorem above is as follows:
定理。设
Theorem. Let
证明。
Proof.
11.3.索引集合族¶
11.3. Indexed Families of Sets¶
如果
If
由自然数索引的集合。这个概念最好通过一些例子来说明。
of sets indexed by the natural numbers. The concept is best illustrated by some examples.
对于每个自然数
,我们可以定义集合 是当今年龄在世的一群人 。对于每个年龄,我们都有相应的集合。20 岁的人是集合中的一个元素 ,而新生婴儿是 . 该套装 是空的。这个家庭 是一个以自然数为索引的集合族。For each natural number
, we can define the set to be the set of people alive today that are of age . For each age we have the corresponding set. Someone of age 20 is an element of the set , while a newborn baby is an element of . The set is empty. This family is a is a family of sets indexed by the natural numbers.对于每个实数
我们可以定义 是大于 , 所以 。 然后 是一个以实数为索引的集合族。For every real number
we can define to be the set of positive real numbers larger than , so . Then is a family of sets indexed by the real numbers.对于每个自然数
我们可以定义 作为除数的集合 。For every natural number
we can define as the set of divisors of .
有了家庭
Given a family
我们还可以形成集合族的交集:
We can also form the intersection of a family of sets:
因此一个元素
So an element
回到上面的例子,我们可以计算每个家族的并集和交集。对于第一个例子,
Returning to the examples above, we can compute the union and intersection of each family. For the first example,
假设
Suppose that
这意味着两个集合的并集与交集只是集合族的并集与交集的一个特例。
This means that the union and intersection of two sets are just a special case of the union and intersection of a family of sets.
我们也有集合族的并集和交集的等式。下面是其中一些:
We also have equalities for unions and intersections of families of sets. Here are a few of them:
在最后两行中,
In the last two lines,
让我们证明第一个恒等式。注意断言的逻辑形式
Let’s prove the first identity. Notice how the logical forms of the assertions
定理。设
Theorem. Let
证明。假设
Proof. Suppose
相反,假设
Conversely, suppose
11.4.笛卡尔积与幂集¶
11.4. Cartesian Product and Power Set¶
两个对象的有序对
The ordered pair of two objects
给定两个集合
Given two sets
请注意,如果
Note that if
一些公理基础将对的概念视为原始概念。在公理集合论中,通常将有序对定义为特定集合,即
Some axiomatic foundations take the notion of a pair to be primitive. In axiomatic set theory, it is common to define an ordered pair to be a particular set, namely
请注意,如果
Notice that if
下面的定理表明这个定义是合理的。
The following theorem shows that this definition is reasonable.
定理。利用上面有序对的定义,我们有
Theorem. Using the definition of ordered pairs above, we have
证明。如果
Proof. If
首先假设
Suppose first that
现在假设
Now suppose that
因此,在这两种情况下,我们得出的结论是
Hence in both cases we conclude that
利用有序对,我们可以定义有序三元组
Using ordered pairs we can define the ordered triple
给定一个集合
Given a set
如果
If
11.5.练习¶
11.5. Exercises¶
证明以下定理:设
, , 和 是某个域的元素集。那么 .(从今以后,如果我们不指定自然演绎或精益,“证明”和“展示”意味着给出一个普通的数学证明,使用普通的数学语言而不是符号逻辑。)Prove the following theorem: Let
, , and be sets of elements of some domain. Then . (Henceforth, if we don’t specify natural deduction or Lean, ``prove’’ and ``show’’ mean give an ordinary mathematical proof, using ordinary mathematical language rather than symbolic logic.)证明以下定理:设
和 是某个域的元素集。那么 。Prove the following theorem: Let
and be sets of elements of some domain. Then .两套
和 如果它们没有共同元素,则称它们不相交。说明如果 和 是不相交的, , 和 , 然后 和 是不相交的。Two sets
and are said to be disjoint if they have no element in common. Show that if and are disjoint, , and , then and are disjoint.让
和 是集合。显示 ,通过表明两边具有相同的元素。Let
and be sets. Show , by showing that both sides have the same elements.让
, , 和 是某个域的子集 . 给出恒等式的计算证明 ,使用上面的恒等式。还可以利用以下事实:一般来说, 。Let
, , and be subsets of some domain . Give a calculational proof of the identity , using the identities above. Also use the fact that, in general, .类似地,给出计算证明
。Similarly, give a calculational proof of
.给出下列计算证明:
Give calculational proofs of the following:
证明如果
是一个由两个集合索引的家庭 和 , 然后另外,找一个家庭
其中逆包含不成立。Prove that if
is a family indexed by two sets and , thenAlso, find a family
where the reverse inclusion does not hold.使用计算推理证明
符号
方法 。Prove using calculational reasoning that
The notation
means .使用定义
,表明 当且仅当 , 和 。Using the definition
, show that if and only if , and .证明
Prove that
证明
. 找到一个表达方式 由笛卡尔积的并集组成,并证明你的表达是正确的。Prove that
. Find an expression for consisting of unions of cartesian products, and prove that your expression is correct.证明
当且仅当 。Prove that that
if and only if .
12.精益中的集合¶
12. Sets in Lean¶
在上一章中,我们注意到,虽然在公理集合论中人们考虑的是不同对象的集合,但在数学中更常见的是考虑某个固定域的子集,U,Lean 都会为我们提供一种新的数据类型 ,它由 的元素集组成。因此,例如,我们可以推理自然数集、整数集或自然数对集。set UU
In the last chapter, we noted that although in axiomatic set theory one considers sets of disparate objects, it is more common in mathematics to consider subsets of some fixed domain, U, Lean gives us a new data type, set U, consisting of the sets of elements of U. Thus, for example, we can reason about sets of natural numbers, or sets of integers, or sets of pairs of natural numbers.
12.1.基础知识¶
12.1. Basics¶
给定和,我们可以写出表示是集合的成员。可以使用来输入字符。A : set Ux : Ux ∈ AxA∈\in
Given A : set U and x : U, we can write x ∈ A to state that x is a member of the set A. The character ∈ can be typed using \in.
import data.set
open set
variable {U : Type}
variables A B C : set U
variable x : U
#check x ∈ A
#check A ∪ B
#check B \ C
#check C ∩ A
#check Cᶜ
#check ∅ ⊆ A
#check B ⊆ univ
import data.set
open set
variable {U : Type}
variables A B C : set U
variable x : U
#check x ∈ A
#check A ∪ B
#check B \ C
#check C ∩ A
#check Cᶜ
#check ∅ ⊆ A
#check B ⊆ univ
您可以将符号⊆、∅、∪、分别输入为、、和。我们将类型变量设为隐式,因为它通常可以从上下文中推断出来。全集表示为,集合补集用上标字母“c”表示,您可以将其输入为∩或。类似这些的基本集合论概念在 Lean 的核心库中定义,但辅助库中提供了其他定理和符号,我们使用命令 加载了该辅助库,该命令必须出现在文件的开头。该命令让我们引用名为的定理。\\subeq \empty\un\i\\Uuniv\^c\complimport data.setopen setset.mem_unionmem_union
You can type the symbols ⊆, ∅, ∪, ∩, \ as \subeq \empty, \un, \i, and \\, respectively. We have made the type variable U implicit, because it can typically be inferred from context. The universal set is denoted univ, and set complementation is denoted with the superscripted letter “c,” which you can enter as \^c or \compl. Basic set-theoretic notions like these are defined in Lean’s core library, but additional theorems and notation are available in an auxiliary library that we have loaded with the command import data.set, which has to appear at the beginning of a file. The command open set lets us refer to a theorem named set.mem_union as mem_union.
下面的模式可以用来表明A是的子集B:
The following pattern can be used to show that A is a subset of B:
example : A ⊆ B :=
assume x,
assume h : x ∈ A,
show x ∈ B, from sorry
example : A ⊆ B :=
assume x,
assume h : x ∈ A,
show x ∈ B, from sorry
并且可以使用以下模式来证明A和B相等:
And the following pattern can be used to show that A and B are equal:
example : A = B :=
eq_of_subset_of_subset
(assume x,
assume h : x ∈ A,
show x ∈ B, from sorry)
(assume x,
assume h : x ∈ B,
show x ∈ A, from sorry)
example : A = B :=
eq_of_subset_of_subset
(assume x,
assume h : x ∈ A,
show x ∈ B, from sorry)
(assume x,
assume h : x ∈ B,
show x ∈ A, from sorry)
或者,我们可以使用以下模式:
Alternatively, we can use the following pattern:
example : A = B :=
ext (assume x, iff.intro
(assume h : x ∈ A,
show x ∈ B, from sorry)
(assume h : x ∈ B,
show x ∈ A, from sorry))
example : A = B :=
ext (assume x, iff.intro
(assume h : x ∈ A,
show x ∈ B, from sorry)
(assume h : x ∈ B,
show x ∈ A, from sorry))
这里,ext是“外延性”的简称。用符号表示,就是以下事实:
Here, ext is short for “extensionality.” In symbolic terms, it is the following fact:
这减少了证明
This reduces proving
此外,Lean 支持以下巧妙的功能:集合上的并集、交集和其他运算的定义规则被认为是“定义性”的。这意味着表达式和对 Lean 来说含义相同。对于集合上的其他构造也是如此;例如和对 Lean 含义相同。您还可以为编写,其中使用 编写。对于其他集合构造,上一章中的定义等价关系定义性地成立。以下示例说明了这些功能。x ∈ A ∩ Bx ∈ A ∧ x ∈ Bx ∈ A \ Bx ∈ A ∧ ¬ (x ∈ B)x ∉ B¬ (x ∈ B)∉\notin
Moreover, Lean supports the following nifty feature: the defining rules for union, intersection and other operations on sets are considered to hold “definitionally.” This means that the expressions x ∈ A ∩ B and x ∈ A ∧ x ∈ B mean the same thing to Lean. This is the same for the other constructions on sets; for example x ∈ A \ B and x ∈ A ∧ ¬ (x ∈ B) mean the same thing to Lean. You can also write x ∉ B for ¬ (x ∈ B), where ∉ is written using \notin. For the other set constructions, the defining equivalences in the last chapter hold definitionally. The following example illustrates these features.
example : ∀ x, x ∈ A → x ∈ B → x ∈ A ∩ B :=
assume x,
assume : x ∈ A,
assume : x ∈ B,
show x ∈ A ∩ B, from and.intro ‹x ∈ A› ‹x ∈ B›
example : A ⊆ A ∪ B :=
assume x,
assume : x ∈ A,
show x ∈ A ∪ B, from or.inl this
example : ∅ ⊆ A :=
assume x,
assume : x ∈ ∅,
show x ∈ A, from false.elim ‹x ∈ (∅ : set U)›
example : ∀ x, x ∈ A → x ∈ B → x ∈ A ∩ B :=
assume x,
assume : x ∈ A,
assume : x ∈ B,
show x ∈ A ∩ B, from and.intro ‹x ∈ A› ‹x ∈ B›
example : A ⊆ A ∪ B :=
assume x,
assume : x ∈ A,
show x ∈ A ∪ B, from or.inl this
example : ∅ ⊆ A :=
assume x,
assume : x ∈ ∅,
show x ∈ A, from false.elim ‹x ∈ (∅ : set U)›
记住第 4.5 节中的内容,我们可以使用不带标签的 ,并使用法语引号引用假设,以和assume开头。我们在上一个示例中使用了此功能。如果没有该功能,我们可以将上述示例编写如下:\f<\f>
Remember from Section 4.5 that we can use assume without a label, and refer back to hypotheses using French quotes, entered with \f< and \f>. We have used this feature in the previous example. Without that feature, we could have written the examples above as follows:
example : ∀ x, x ∈ A → x ∈ B → x ∈ A ∩ B :=
assume x,
assume h1 : x ∈ A,
assume h2 : x ∈ B,
show x ∈ A ∩ B, from and.intro h1 h2
example : A ⊆ A ∪ B :=
assume x,
assume h : x ∈ A,
show x ∈ A ∪ B, from or.inl h
example : ∅ ⊆ A :=
assume x,
assume h : x ∈ (∅ : set U),
show x ∈ A, from false.elim h
example : ∀ x, x ∈ A → x ∈ B → x ∈ A ∩ B :=
assume x,
assume h1 : x ∈ A,
assume h2 : x ∈ B,
show x ∈ A ∩ B, from and.intro h1 h2
example : A ⊆ A ∪ B :=
assume x,
assume h : x ∈ A,
show x ∈ A ∪ B, from or.inl h
example : ∅ ⊆ A :=
assume x,
assume h : x ∈ (∅ : set U),
show x ∈ A, from false.elim h
下面,以及在随后的章节中,我们将开始使用不带标签assume的have命令,但您可以自由采用您喜欢的任何风格。
Below, and in the chapters that follow, we will begin to use assume and have command without labels, but you should feel free to adopt whatever style you prefer.
还要注意,在上一个例子中,我们必须通过书写来注释空集,以告诉 Lean 我们指的是哪个空集。Lean 通常可以从上下文中推断出这样的信息(例如,从我们试图展示的事实来看,其中类型为),但在这种情况下,它需要更多的帮助。(∅ : set U)x ∈ AAset U
Notice also that in the last example, we had to annotate the empty set by writing (∅ : set U) to tell Lean which empty set we mean. Lean can often infer information like this from the context (for example, from the fact that we are trying to show x ∈ A, where A has type set U), but in this case, it needs a bit more help.
或者,我们可以使用 Lean 库中专为集合设计的定理:
Alternatively, we can use theorems in the Lean library that are designed specifically for use with sets:
example : ∀ x, x ∈ A → x ∈ B → x ∈ A ∩ B :=
assume x,
assume : x ∈ A,
assume : x ∈ B,
show x ∈ A ∩ B, from mem_inter ‹x ∈ A› ‹x ∈ B›
example : A ⊆ A ∪ B :=
assume x,
assume h : x ∈ A,
show x ∈ A ∪ B, from mem_union_left B h
example : ∅ ⊆ A :=
assume x,
assume : x ∈ ∅,
show x ∈ A, from absurd this (not_mem_empty x)
example : ∀ x, x ∈ A → x ∈ B → x ∈ A ∩ B :=
assume x,
assume : x ∈ A,
assume : x ∈ B,
show x ∈ A ∩ B, from mem_inter ‹x ∈ A› ‹x ∈ B›
example : A ⊆ A ∪ B :=
assume x,
assume h : x ∈ A,
show x ∈ A ∪ B, from mem_union_left B h
example : ∅ ⊆ A :=
assume x,
assume : x ∈ ∅,
show x ∈ A, from absurd this (not_mem_empty x)
请记住,absurd可用于从两个相互矛盾的假设和证明任何事实。 这里是事实。 您可以使用Lean 中的命令查看定理的陈述:h1 : Ph2 : ¬ Pnot_mem_empty xx ∉ ∅#check
Remember that absurd can be used to prove any fact from two contradictory hypotheses h1 : P and h2 : ¬ P. Here the not_mem_empty x is the fact x ∉ ∅. You can see the statements of the theorems using the #check command in Lean:
#check @mem_inter
#check @mem_of_mem_inter_left
#check @mem_of_mem_inter_right
#check @mem_union_left
#check @mem_union_right
#check @mem_or_mem_of_mem_union
#check @not_mem_empty
#check @mem_inter
#check @mem_of_mem_inter_left
#check @mem_of_mem_inter_right
#check @mem_union_left
#check @mem_union_right
#check @mem_or_mem_of_mem_union
#check @not_mem_empty
这里,@Lean 中的符号阻止它尝试自动填充隐式参数,迫使它显示定理的完整陈述。
Here, the @ symbol in Lean prevents it from trying to fill in implicit arguments automatically, forcing it to display the full statement of the theorem.
Lean 可以通过逻辑定义来识别集合,这使得证明集合之间的包含关系变得容易:
The fact that Lean can identify sets with their logical definitions makes it easy to prove inclusions between sets:
example : A \ B ⊆ A :=
assume x,
assume : x ∈ A \ B,
show x ∈ A, from and.left this
example : A \ B ⊆ Bᶜ :=
assume x,
assume : x ∈ A \ B,
have x ∉ B, from and.right this,
show x ∈ Bᶜ, from this
example : A \ B ⊆ A :=
assume x,
assume : x ∈ A \ B,
show x ∈ A, from and.left this
example : A \ B ⊆ Bᶜ :=
assume x,
assume : x ∈ A \ B,
have x ∉ B, from and.right this,
show x ∈ Bᶜ, from this
再次,我们可以使用专门为集合设计的定理:
Once again, we can use the theorems designed specifically for sets:
example : A \ B ⊆ A :=
assume x,
assume : x ∈ A \ B,
show x ∈ A, from mem_of_mem_diff this
example : A \ B ⊆ Bᶜ :=
assume x,
assume : x ∈ A \ B,
have x ∉ B, from not_mem_of_mem_diff this,
show x ∈ Bᶜ, from this
example : A \ B ⊆ A :=
assume x,
assume : x ∈ A \ B,
show x ∈ A, from mem_of_mem_diff this
example : A \ B ⊆ Bᶜ :=
assume x,
assume : x ∈ A \ B,
have x ∉ B, from not_mem_of_mem_diff this,
show x ∈ Bᶜ, from this
Lean 必须展开定义,这意味着它有时会令人困惑。例如,在下面的证明中,如果你将最后一行替换为sorry,Lean 很难弄清楚你希望它展开子集符号:
The fact that Lean has to unfold definitions means that it can be confused at times. For example, in the proof below, if you replace the last line by sorry, Lean has trouble figuring out that you want it to unfold the subset symbol:
variable {U : Type}
variables A B : set U
example : A ∩ B ⊆ B ∩ A :=
assume x,
assume h : x ∈ A ∩ B,
have h1 : x ∈ A, from and.left h,
have h2 : x ∈ B, from and.right h,
and.intro h2 h1
variable {U : Type}
variables A B : set U
example : A ∩ B ⊆ B ∩ A :=
assume x,
assume h : x ∈ A ∩ B,
have h1 : x ∈ A, from and.left h,
have h2 : x ∈ B, from and.right h,
and.intro h2 h1
一种解决方法是使用show命令;一般来说,向 Lean 提供此类附加信息通常很有帮助。另一种解决方法是给定理命名,这会促使 Lean 使用略有不同的方法来处理证明,从而将问题作为幸运的副作用解决。
One workaround is to use the show command; in general, providing Lean with such additional information is often helpful. Another workaround is to give the theorem a name, which prompts Lean to use a slightly different method of processing the proof, fixing the problem as a lucky side effect.
example : A ∩ B ⊆ B ∩ A :=
assume x,
assume h : x ∈ A ∩ B,
have h1 : x ∈ A, from and.left h,
have h2 : x ∈ B, from and.right h,
show x ∈ B ∩ A, from sorry
theorem my_example : A ∩ B ⊆ B ∩ A :=
assume x,
assume h : x ∈ A ∩ B,
have h1 : x ∈ A, from and.left h,
have h2 : x ∈ B, from and.right h,
sorry
example : A ∩ B ⊆ B ∩ A :=
assume x,
assume h : x ∈ A ∩ B,
have h1 : x ∈ A, from and.left h,
have h2 : x ∈ B, from and.right h,
show x ∈ B ∩ A, from sorry
theorem my_example : A ∩ B ⊆ B ∩ A :=
assume x,
assume h : x ∈ A ∩ B,
have h1 : x ∈ A, from and.left h,
have h2 : x ∈ B, from and.right h,
sorry
12.2.一些身份¶
12.2. Some Identities¶
这是我们在上一章中非正式证明的第一个恒等式的证明:
Here is the proof of the first identity that we proved informally in the previous chapter:
example : A ∩ (B ∪ C) = (A ∩ B) ∪ (A ∩ C) :=
eq_of_subset_of_subset
(assume x,
assume h : x ∈ A ∩ (B ∪ C),
have x ∈ A, from and.left h,
have x ∈ B ∪ C, from and.right h,
or.elim (‹x ∈ B ∪ C›)
(assume : x ∈ B,
have x ∈ A ∩ B, from and.intro ‹x ∈ A› ‹x ∈ B›,
show x ∈ (A ∩ B) ∪ (A ∩ C), from or.inl this)
(assume : x ∈ C,
have x ∈ A ∩ C, from and.intro ‹x ∈ A› ‹x ∈ C›,
show x ∈ (A ∩ B) ∪ (A ∩ C), from or.inr this))
(assume x,
assume : x ∈ (A ∩ B) ∪ (A ∩ C),
or.elim this
(assume h : x ∈ A ∩ B,
have x ∈ A, from and.left h,
have x ∈ B, from and.right h,
have x ∈ B ∪ C, from or.inl this,
show x ∈ A ∩ (B ∪ C), from and.intro ‹x ∈ A› this)
(assume h : x ∈ A ∩ C,
have x ∈ A, from and.left h,
have x ∈ C, from and.right h,
have x ∈ B ∪ C, from or.inr this,
show x ∈ A ∩ (B ∪ C), from and.intro ‹x ∈ A› this))
example : A ∩ (B ∪ C) = (A ∩ B) ∪ (A ∩ C) :=
eq_of_subset_of_subset
(assume x,
assume h : x ∈ A ∩ (B ∪ C),
have x ∈ A, from and.left h,
have x ∈ B ∪ C, from and.right h,
or.elim (‹x ∈ B ∪ C›)
(assume : x ∈ B,
have x ∈ A ∩ B, from and.intro ‹x ∈ A› ‹x ∈ B›,
show x ∈ (A ∩ B) ∪ (A ∩ C), from or.inl this)
(assume : x ∈ C,
have x ∈ A ∩ C, from and.intro ‹x ∈ A› ‹x ∈ C›,
show x ∈ (A ∩ B) ∪ (A ∩ C), from or.inr this))
(assume x,
assume : x ∈ (A ∩ B) ∪ (A ∩ C),
or.elim this
(assume h : x ∈ A ∩ B,
have x ∈ A, from and.left h,
have x ∈ B, from and.right h,
have x ∈ B ∪ C, from or.inl this,
show x ∈ A ∩ (B ∪ C), from and.intro ‹x ∈ A› this)
(assume h : x ∈ A ∩ C,
have x ∈ A, from and.left h,
have x ∈ C, from and.right h,
have x ∈ B ∪ C, from or.inr this,
show x ∈ A ∩ (B ∪ C), from and.intro ‹x ∈ A› this))
请注意,它比上一章中的非正式证明长得多,因为我们已经详细说明了每一个细节。不幸的是,这并不一定会使其更具可读性。请记住,您可以随时使用逐步编写长证明sorry。您还可以将长证明分解成较小的部分:
Notice that it is considerably longer than the informal proof in the last chapter, because we have spelled out every last detail. Unfortunately, this does not necessarily make it more readable. Keep in mind that you can always write long proofs incrementally, using sorry. You can also break up long proofs into smaller pieces:
theorem inter_union_subset : A ∩ (B ∪ C) ⊆ (A ∩ B) ∪ (A ∩ C) :=
assume x,
assume h : x ∈ A ∩ (B ∪ C),
have x ∈ A, from and.left h,
have x ∈ B ∪ C, from and.right h,
or.elim (‹x ∈ B ∪ C›)
(assume : x ∈ B,
have x ∈ A ∩ B, from and.intro ‹x ∈ A› ‹x ∈ B›,
show x ∈ (A ∩ B) ∪ (A ∩ C), from or.inl this)
(assume : x ∈ C,
have x ∈ A ∩ C, from and.intro ‹x ∈ A› ‹x ∈ C›,
show x ∈ (A ∩ B) ∪ (A ∩ C), from or.inr this)
theorem inter_union_inter_subset :
(A ∩ B) ∪ (A ∩ C) ⊆ A ∩ (B ∪ C) :=
assume x,
assume : x ∈ (A ∩ B) ∪ (A ∩ C),
or.elim this
(assume h : x ∈ A ∩ B,
have x ∈ A, from and.left h,
have x ∈ B, from and.right h,
have x ∈ B ∪ C, from or.inl this,
show x ∈ A ∩ (B ∪ C), from and.intro ‹x ∈ A› this)
(assume h : x ∈ A ∩ C,
have x ∈ A, from and.left h,
have x ∈ C, from and.right h,
have x ∈ B ∪ C, from or.inr this,
show x ∈ A ∩ (B ∪ C), from and.intro ‹x ∈ A› this)
example : A ∩ (B ∪ C) = (A ∩ B) ∪ (A ∩ C) :=
eq_of_subset_of_subset
(inter_union_subset A B C)
(inter_union_inter_subset A B C)
theorem inter_union_subset : A ∩ (B ∪ C) ⊆ (A ∩ B) ∪ (A ∩ C) :=
assume x,
assume h : x ∈ A ∩ (B ∪ C),
have x ∈ A, from and.left h,
have x ∈ B ∪ C, from and.right h,
or.elim (‹x ∈ B ∪ C›)
(assume : x ∈ B,
have x ∈ A ∩ B, from and.intro ‹x ∈ A› ‹x ∈ B›,
show x ∈ (A ∩ B) ∪ (A ∩ C), from or.inl this)
(assume : x ∈ C,
have x ∈ A ∩ C, from and.intro ‹x ∈ A› ‹x ∈ C›,
show x ∈ (A ∩ B) ∪ (A ∩ C), from or.inr this)
theorem inter_union_inter_subset :
(A ∩ B) ∪ (A ∩ C) ⊆ A ∩ (B ∪ C) :=
assume x,
assume : x ∈ (A ∩ B) ∪ (A ∩ C),
or.elim this
(assume h : x ∈ A ∩ B,
have x ∈ A, from and.left h,
have x ∈ B, from and.right h,
have x ∈ B ∪ C, from or.inl this,
show x ∈ A ∩ (B ∪ C), from and.intro ‹x ∈ A› this)
(assume h : x ∈ A ∩ C,
have x ∈ A, from and.left h,
have x ∈ C, from and.right h,
have x ∈ B ∪ C, from or.inr this,
show x ∈ A ∩ (B ∪ C), from and.intro ‹x ∈ A› this)
example : A ∩ (B ∪ C) = (A ∩ B) ∪ (A ∩ C) :=
eq_of_subset_of_subset
(inter_union_subset A B C)
(inter_union_inter_subset A B C)
请注意,这两个定理依赖于变量A、B和C,在应用它们时必须将它们作为参数提供。它们还依赖于底层类型 ,U但由于变量U被标记为隐式,因此 Lean 会根据上下文来推断。
Notice that the two theorems depend on the variables A, B, and C, which have to be supplied as arguments when they are applied. They also depend on the underlying type, U, but because the variable U was marked implicit, Lean figures it out from the context.
在上一章中,我们展示了
In the last chapter, we showed
example : (A ∩ Bᶜ) ∪ B = A ∪ B :=
calc
(A ∩ Bᶜ) ∪ B = (A ∪ B) ∩ (Bᶜ ∪ B) : by rw union_distrib_right
... = (A ∪ B) ∩ univ : by rw compl_union_self
... = A ∪ B : by rw inter_univ
example : (A ∩ Bᶜ) ∪ B = A ∪ B :=
calc
(A ∩ Bᶜ) ∪ B = (A ∪ B) ∩ (Bᶜ ∪ B) : by rw union_distrib_right
... = (A ∪ B) ∩ univ : by rw compl_union_self
... = A ∪ B : by rw inter_univ
转化为命题,上述定理指出,对于每一对元素↔。
Translated to propositions, the theorem above states that for every pair of elements ↔.
variables A B : Prop
example : (A ∧ ¬ B) ∨ B ↔ A ∨ B :=
calc
(A ∧ ¬ B) ∨ B ↔ (A ∨ B) ∧ (¬ B ∨ B) : by rw and_or_distrib_right
... ↔ (A ∨ B) ∧ true : by rw not_or_self
... ↔ (A ∨ B) : by rw and_true
variables A B : Prop
example : (A ∧ ¬ B) ∨ B ↔ A ∨ B :=
calc
(A ∧ ¬ B) ∨ B ↔ (A ∨ B) ∧ (¬ B ∨ B) : by rw and_or_distrib_right
... ↔ (A ∨ B) ∧ true : by rw not_or_self
... ↔ (A ∨ B) : by rw and_true
12.3.索引家族¶
12.3. Indexed Families¶
请记住,如果A是集合族,其中是。换句话说,集合族实际上是一个函数,它对类型的每个元素返回一个集合。然后我们可以定义并集和交集如下:A : I → set UITypeiIA i
Remember that if A is a family of sets by writing A : I → set U where I is a Type. In other words, a family of sets is really a function which for each element i of type I returns a set A i. We can then define the union and intersection as follows:
variables {I U : Type}
def Union (A : I → set U) : set U := { x | ∃ i : I, x ∈ A i }
def Inter (A : I → set U) : set U := { x | ∀ i : I, x ∈ A i }
section
variables (x : U) (A : I → set U)
example (h : x ∈ Union A) : ∃ i, x ∈ A i := h
example (h : x ∈ Inter A) : ∀ i, x ∈ A i := h
end
variables {I U : Type}
def Union (A : I → set U) : set U := { x | ∃ i : I, x ∈ A i }
def Inter (A : I → set U) : set U := { x | ∀ i : I, x ∈ A i }
section
variables (x : U) (A : I → set U)
example (h : x ∈ Union A) : ∃ i, x ∈ A i := h
example (h : x ∈ Inter A) : ∀ i, x ∈ A i := h
end
这些示例表明,Lean 可以展开定义,以便可以视为,可以视为。要重温如何使用 Lean 中的通用量词和存在量词,请参阅第 9 章。然后我们可以定义索引并集和交集的符号:x ∈ Inter A∀ i, x ∈ A ix ∈ Union A∃ i, x ∈ A i
The examples show that Lean can unfold the definitions so that x ∈ Inter A can be treated as ∀ i, x ∈ A i and x ∈ Union A can be treated as ∃ i, x ∈ A i. To refresh your memory as to how to work with the universal and existential quantifiers in Lean, see Chapters 9. We can then define notation for the indexed union and intersection:
notation `⋃` binders `, ` r:(scoped f, Union f) := r
notation `⋂` binders `, ` r:(scoped f, Inter f) := r
variables (A : I → set U) (x : U)
example (h : x ∈ ⋃ i, A i) : ∃ i, x ∈ A i := h
example (h : x ∈ ⋂ i, A i) : ∀ i, x ∈ A i := h
notation `⋃` binders `, ` r:(scoped f, Union f) := r
notation `⋂` binders `, ` r:(scoped f, Inter f) := r
variables (A : I → set U) (x : U)
example (h : x ∈ ⋃ i, A i) : ∃ i, x ∈ A i := h
example (h : x ∈ ⋂ i, A i) : ∀ i, x ∈ A i := h
您可以分别将⋂和键入⋃为\I和\Un。与量词一样,符号和绑定表达式中的变量,并且范围尽可能广泛。例如,如果您写,Lean 会假定序列的第 i 个元素是。如果您想将范围限制得更窄,请使用括号。⋃ i, A i⋂ i, A ii⋂ i, A i ∪ BA i ∪ B
You can type ⋂ and ⋃ with \I and \Un, respectively. As with quantifiers, the notation ⋃ i, A i and ⋂ i, A i bind the variable i in the expression, and the scope extends as widely as possible. For example, if you write ⋂ i, A i ∪ B, Lean assumes that the ith element of the sequence is A i ∪ B. If you want to restrict the scope more narrowly, use parentheses.
好消息是,Lean 的库确实用这种符号定义了索引并集和交集,并且这些定义可以通过 获得。坏消息是,它使用了不同的定义,因此和在定义上不等于和,如上所述。好消息是 Lean 至少知道它们是等价的:import data.setx ∈ Inter Ax ∈ Union A∀ i, x ∈ A i∃ i, x ∈ A i
The good news is that Lean’s library does define indexed union and intersection, with this notation, and the definitions are made available with import data.set. The bad news is that it uses a different definition, so that x ∈ Inter A and x ∈ Union A are not definitionally equal to ∀ i, x ∈ A i and ∃ i, x ∈ A i, as above. The good news is that Lean at least knows that they are equivalent:
import data.set
open set
variables {I U : Type}
variables {A B : I → set U}
theorem exists_of_mem_Union {x : U} (h : x ∈ ⋃ i, A i) :
∃ i, x ∈ A i :=
by simp * at *
theorem mem_Union_of_exists {x : U} (h : ∃ i, x ∈ A i) :
x ∈ ⋃ i, A i :=
by simp * at *
theorem forall_of_mem_Inter {x : U} (h : x ∈ ⋂ i, A i) :
∀ i, x ∈ A i :=
by simp * at *
theorem mem_Inter_of_forall {x : U} (h : ∀ i, x ∈ A i) :
x ∈ ⋂ i, A i :=
by simp * at *
import data.set
open set
variables {I U : Type}
variables {A B : I → set U}
theorem exists_of_mem_Union {x : U} (h : x ∈ ⋃ i, A i) :
∃ i, x ∈ A i :=
by simp * at *
theorem mem_Union_of_exists {x : U} (h : ∃ i, x ∈ A i) :
x ∈ ⋃ i, A i :=
by simp * at *
theorem forall_of_mem_Inter {x : U} (h : x ∈ ⋂ i, A i) :
∀ i, x ∈ A i :=
by simp * at *
theorem mem_Inter_of_forall {x : U} (h : ∀ i, x ∈ A i) :
x ∈ ⋂ i, A i :=
by simp * at *
该命令调用 Lean 的自动化程序来执行校样。以下是如何使用它们的一个示例:simp * at *
The command simp * at * calls upon Lean’s automation to carry out the proofs. Here is an example of how these can be used:
example : (⋂ i, A i ∩ B i) = (⋂ i, A i) ∩ (⋂ i, B i) :=
ext $
assume x : U,
iff.intro
(assume h : x ∈ ⋂ i, A i ∩ B i,
have h1 : ∀ i, x ∈ A i ∩ B i, from forall_of_mem_Inter h,
have h2 : ∀ i, x ∈ A i, from assume i, and.left (h1 i),
have h3 : ∀ i, x ∈ B i, from assume i, and.right (h1 i),
have h4 : x ∈ ⋂ i, A i, from mem_Inter_of_forall h2,
have h5 : x ∈ ⋂ i, B i, from mem_Inter_of_forall h3,
and.intro h4 h5)
(assume h : x ∈ (⋂ i, A i) ∩ (⋂ i, B i),
have h1 : ∀ i, x ∈ A i,
from forall_of_mem_Inter (and.left h),
have h2 : ∀ i, x ∈ B i,
from forall_of_mem_Inter (and.right h),
have h3 : ∀ i, x ∈ A i ∩ B i,
from assume i, and.intro (h1 i) (h2 i),
show x ∈ ⋂ i, A i ∩ B i, from mem_Inter_of_forall h3)
example : (⋂ i, A i ∩ B i) = (⋂ i, A i) ∩ (⋂ i, B i) :=
ext $
assume x : U,
iff.intro
(assume h : x ∈ ⋂ i, A i ∩ B i,
have h1 : ∀ i, x ∈ A i ∩ B i, from forall_of_mem_Inter h,
have h2 : ∀ i, x ∈ A i, from assume i, and.left (h1 i),
have h3 : ∀ i, x ∈ B i, from assume i, and.right (h1 i),
have h4 : x ∈ ⋂ i, A i, from mem_Inter_of_forall h2,
have h5 : x ∈ ⋂ i, B i, from mem_Inter_of_forall h3,
and.intro h4 h5)
(assume h : x ∈ (⋂ i, A i) ∩ (⋂ i, B i),
have h1 : ∀ i, x ∈ A i,
from forall_of_mem_Inter (and.left h),
have h2 : ∀ i, x ∈ B i,
from forall_of_mem_Inter (and.right h),
have h3 : ∀ i, x ∈ A i ∩ B i,
from assume i, and.intro (h1 i) (h2 i),
show x ∈ ⋂ i, A i ∩ B i, from mem_Inter_of_forall h3)
更好的是,我们可以证明交集和并集的引入和消除规则:
Even better, we can prove introduction and elimination rules for intersection and union:
import data.set
open set
variables {I U : Type}
variables {A : I → set U}
theorem Inter.intro {x : U} (h : ∀ i, x ∈ A i) : x ∈ ⋂ i, A i :=
by simp; assumption
@[elab_simple]
theorem Inter.elim {x : U} (h : x ∈ ⋂ i, A i) (i : I) : x ∈ A i :=
by simp at h; apply h
theorem Union.intro {x : U} (i : I) (h : x ∈ A i) :
x ∈ ⋃ i, A i :=
by {simp, existsi i, exact h}
theorem Union.elim {b : Prop} {x : U}
(h₁ : x ∈ ⋃ i, A i) (h₂ : ∀ (i : I), x ∈ A i → b) : b :=
by {simp at h₁, cases h₁ with i h, exact h₂ i h}
import data.set
open set
variables {I U : Type}
variables {A : I → set U}
theorem Inter.intro {x : U} (h : ∀ i, x ∈ A i) : x ∈ ⋂ i, A i :=
by simp; assumption
@[elab_simple]
theorem Inter.elim {x : U} (h : x ∈ ⋂ i, A i) (i : I) : x ∈ A i :=
by simp at h; apply h
theorem Union.intro {x : U} (i : I) (h : x ∈ A i) :
x ∈ ⋃ i, A i :=
by {simp, existsi i, exact h}
theorem Union.elim {b : Prop} {x : U}
(h₁ : x ∈ ⋃ i, A i) (h₂ : ∀ (i : I), x ∈ A i → b) : b :=
by {simp at h₁, cases h₁ with i h, exact h₂ i h}
不要担心证明的意义。重要的是如何使用它们,这就是以下模式所说明的:
Don’t worry about what the proofs mean. What is important is how they can be used, which is what the following patterns illustrate:
example (x : U) : x ∈ ⋂ i, A i :=
Inter.intro $
assume i,
show x ∈ A i, from sorry
example (x : U) (i : I) (h : x ∈ ⋂ i, A i) : x ∈ A i :=
Inter.elim h i
example (x : U) (i : I) (h : x ∈ A i) : x ∈ ⋃ i, A i :=
Union.intro i h
example (C : Prop) (x : U) (h : x ∈ ⋃ i, A i) : C :=
Union.elim h $
assume i,
assume h : x ∈ A i,
show C, from sorry
example (x : U) : x ∈ ⋂ i, A i :=
Inter.intro $
assume i,
show x ∈ A i, from sorry
example (x : U) (i : I) (h : x ∈ ⋂ i, A i) : x ∈ A i :=
Inter.elim h i
example (x : U) (i : I) (h : x ∈ A i) : x ∈ ⋃ i, A i :=
Union.intro i h
example (C : Prop) (x : U) (h : x ∈ ⋃ i, A i) : C :=
Union.elim h $
assume i,
assume h : x ∈ A i,
show C, from sorry
请记住,美元符号使我们免去了在其余证明周围加上括号的麻烦。请注意,对于Inter.intro和Inter.elim,使用索引交集的证明看起来就像使用全称量词的证明一样。同样,Union.intro和Union.elim反映了存在量词的引入和消除规则。以下示例提供了上面证明的等价关系的一个方向:
Remember that the dollar sign saves us the trouble of having to put parentheses around the rest of the proof. Notice that with Inter.intro and Inter.elim, proofs using indexed intersections looks just like proofs using the universal quantifier. Similarly, Union.intro and Union.elim mirror the introduction and elimination rules for the existential quantifier. The following example provides one direction of an equivalence proved above:
variables {I U : Type}
variables (A : I → set U) (B : I → set U) (C : set U)
example : (⋂ i, A i ∩ B i) ⊆ (⋂ i, A i) ∩ (⋂ i, B i) :=
assume x : U,
assume h : x ∈ ⋂ i, A i ∩ B i,
have h1 : x ∈ ⋂ i, A i, from
Inter.intro $
assume i : I,
have h2 : x ∈ A i ∩ B i, from Inter.elim h i,
show x ∈ A i, from and.left h2,
have h2 : x ∈ ⋂ i, B i, from
Inter.intro $
assume i : I,
have h2 : x ∈ A i ∩ B i, from Inter.elim h i,
show x ∈ B i, from and.right h2,
show x ∈ (⋂ i, A i) ∩ (⋂ i, B i), from and.intro h1 h2
variables {I U : Type}
variables (A : I → set U) (B : I → set U) (C : set U)
example : (⋂ i, A i ∩ B i) ⊆ (⋂ i, A i) ∩ (⋂ i, B i) :=
assume x : U,
assume h : x ∈ ⋂ i, A i ∩ B i,
have h1 : x ∈ ⋂ i, A i, from
Inter.intro $
assume i : I,
have h2 : x ∈ A i ∩ B i, from Inter.elim h i,
show x ∈ A i, from and.left h2,
have h2 : x ∈ ⋂ i, B i, from
Inter.intro $
assume i : I,
have h2 : x ∈ A i ∩ B i, from Inter.elim h i,
show x ∈ B i, from and.right h2,
show x ∈ (⋂ i, A i) ∩ (⋂ i, B i), from and.intro h1 h2
下面的练习要求您证明另一个方向。下面是一个示例,展示了如何使用索引并集的引入和消除规则:
You are asked to prove the other direction in the exercises below. Here is an example that shows how to use the introduction and elimination rules for indexed union:
variables {I U : Type}
variables (A : I → set U) (B : I → set U) (C : set U)
example : (⋃ i, C ∩ A i) ⊆ C ∩ (⋃i, A i) :=
assume x,
assume h : x ∈ ⋃ i, C ∩ A i,
Union.elim h $
assume i,
assume h1 : x ∈ C ∩ A i,
have h2 : x ∈ C, from and.left h1,
have h3 : x ∈ A i, from and.right h1,
have h4 : x ∈ ⋃ i, A i, from Union.intro i h3,
show x ∈ C ∩ ⋃ i, A i, from and.intro h2 h4
variables {I U : Type}
variables (A : I → set U) (B : I → set U) (C : set U)
example : (⋃ i, C ∩ A i) ⊆ C ∩ (⋃i, A i) :=
assume x,
assume h : x ∈ ⋃ i, C ∩ A i,
Union.elim h $
assume i,
assume h1 : x ∈ C ∩ A i,
have h2 : x ∈ C, from and.left h1,
have h3 : x ∈ A i, from and.right h1,
have h4 : x ∈ ⋃ i, A i, from Union.intro i h3,
show x ∈ C ∩ ⋃ i, A i, from and.intro h2 h4
再次,我们要求您在下面的练习中证明另一个方向。
Once again, we ask you to prove the other direction in the exercises below.
有时我们想与家人合作A : I → J → set Ui : Ij : JA i j : set UI → J → set UI → (J → set U)A iJ → set UA i jset U
Sometimes we want to work with families A : I → J → set U, then given i : I and j : J, we have that A i j : set U. (You should interpret the expression I → J → set U as I → (J → set U), so that A i has type J → set U, and then A i j has type set U.) Here is an example of a proof involving a such a doubly-indexed family:
section
variables {I J U : Type}
variables (A : I → J → set U)
example : (⋃i, ⋂j, A i j) ⊆ (⋂j, ⋃i, A i j) :=
assume x,
assume h : x ∈ ⋃i, ⋂j, A i j,
Union.elim h $
assume i,
assume h1 : x ∈ ⋂ j, A i j,
show x ∈ ⋂j, ⋃i, A i j, from
Inter.intro $
assume j,
have h2 : x ∈ A i j, from Inter.elim h1 j,
Union.intro i h2
end
section
variables {I J U : Type}
variables (A : I → J → set U)
example : (⋃i, ⋂j, A i j) ⊆ (⋂j, ⋃i, A i j) :=
assume x,
assume h : x ∈ ⋃i, ⋂j, A i j,
Union.elim h $
assume i,
assume h1 : x ∈ ⋂ j, A i j,
show x ∈ ⋂j, ⋃i, A i j, from
Inter.intro $
assume j,
have h2 : x ∈ A i j, from Inter.elim h1 j,
Union.intro i h2
end
12.4.幂集¶
12.4. Power Sets¶
我们还可以在 Lean 中定义功率集:
We can also define the power set in Lean:
variable {U : Type}
def powerset (A : set U) : set (set U) := {B : set U | B ⊆ A}
example (A B : set U) (h : B ∈ powerset A) : B ⊆ A :=
h
variable {U : Type}
def powerset (A : set U) : set (set U) := {B : set U | B ⊆ A}
example (A B : set U) (h : B ∈ powerset A) : B ⊆ A :=
h
正如示例所示,在定义上与 相同。B ∈ powerset AB ⊆ A
As the example shows, B ∈ powerset A is then definitionally the same as B ⊆ A.
事实上,powerset在 Lean 中正是如此定义的,当你和时,你可以使用它。下面是如何使用它的一个例子:import data.setopen set
In fact, powerset is defined in Lean in exactly this way, and is available to you when you import data.set and open set. Here is an example of how it is used:
#check powerset A
example : A ∈ powerset (A ∪ B) :=
assume x,
assume : x ∈ A,
show x ∈ A ∪ B, from or.inl ‹x ∈ A›
#check powerset A
example : A ∈ powerset (A ∪ B) :=
assume x,
assume : x ∈ A,
show x ∈ A ∪ B, from or.inl ‹x ∈ A›
本质上,该示例证明了。在下面的练习中,我们要求您正式证明,对于每个,我们有A ⊆ A ∪ BA B : set Upowerset A ⊆ powerset B
In essence, the example proves A ⊆ A ∪ B. In the exercises below, we ask you to prove, formally, that for every A B : set U, we have powerset A ⊆ powerset B
12.5.练习¶
12.5. Exercises¶
填写
sorry。example : ∀ x, x ∈ A ∩ C → x ∈ A ∪ B := sorry example : ∀ x, x ∈ (A ∪ B)ᶜ → x ∈ Aᶜ := sorry
Fill in the
sorry’s.example : ∀ x, x ∈ A ∩ C → x ∈ A ∪ B := sorry example : ∀ x, x ∈ (A ∪ B)ᶜ → x ∈ Aᶜ := sorry
填写
sorry。import data.set open set section variable {U : Type} /- defining "disjoint" -/ def disj (A B : set U) : Prop := ∀ ⦃x⦄, x ∈ A → x ∈ B → false example (A B : set U) (h : ∀ x, ¬ (x ∈ A ∧ x ∈ B)) : disj A B := assume x, assume h1 : x ∈ A, assume h2 : x ∈ B, have h3 : x ∈ A ∧ x ∈ B, from and.intro h1 h2, show false, from h x h3 -- notice that we do not have to mention x when applying -- h : disj A B example (A B : set U) (h1 : disj A B) (x : U) (h2 : x ∈ A) (h3 : x ∈ B) : false := h1 h2 h3 -- the same is true of ⊆ example (A B : set U) (x : U) (h : A ⊆ B) (h1 : x ∈ A) : x ∈ B := h h1 example (A B C D : set U) (h1 : disj A B) (h2 : C ⊆ A) (h3 : D ⊆ B) : disj C D := sorry end
Fill in the
sorry.import data.set open set section variable {U : Type} /- defining "disjoint" -/ def disj (A B : set U) : Prop := ∀ ⦃x⦄, x ∈ A → x ∈ B → false example (A B : set U) (h : ∀ x, ¬ (x ∈ A ∧ x ∈ B)) : disj A B := assume x, assume h1 : x ∈ A, assume h2 : x ∈ B, have h3 : x ∈ A ∧ x ∈ B, from and.intro h1 h2, show false, from h x h3 -- notice that we do not have to mention x when applying -- h : disj A B example (A B : set U) (h1 : disj A B) (x : U) (h2 : x ∈ A) (h3 : x ∈ B) : false := h1 h2 h3 -- the same is true of ⊆ example (A B : set U) (x : U) (h : A ⊆ B) (h1 : x ∈ A) : x ∈ B := h h1 example (A B C D : set U) (h1 : disj A B) (h2 : C ⊆ A) (h3 : D ⊆ B) : disj C D := sorry end
利用上面列出的定理
Inter.intro、、Inter.elim和Union.intro,证明有关索引并集和交集的以下事实。Union.elimvariables {I U : Type} variables (A : I → set U) (B : I → set U) (C : set U) example : (⋂ i, A i) ∩ (⋂ i, B i) ⊆ (⋂ i, A i ∩ B i) := sorry example : C ∩ (⋃i, A i) ⊆ ⋃i, C ∩ A i := sorry
Prove the following facts about indexed unions and intersections, using the theorems
Inter.intro,Inter.elim,Union.intro, andUnion.elimlisted above.variables {I U : Type} variables (A : I → set U) (B : I → set U) (C : set U) example : (⋂ i, A i) ∩ (⋂ i, B i) ⊆ (⋂ i, A i ∩ B i) := sorry example : C ∩ (⋃i, A i) ⊆ ⋃i, C ∩ A i := sorry
证明以下关于幂集的事实。你可以使用定理
subset.trans和subset.reflvariable {U : Type} variables A B C : set U -- For this exercise these two facts are useful example (h1 : A ⊆ B) (h2 : B ⊆ C) : A ⊆ C := subset.trans h1 h2 example : A ⊆ A := subset.refl A example (h : A ⊆ B) : powerset A ⊆ powerset B := sorry example (h : powerset A ⊆ powerset B) : A ⊆ B := sorry
Prove the following fact about power sets. You can use the theorems
subset.transandsubset.reflvariable {U : Type} variables A B C : set U -- For this exercise these two facts are useful example (h1 : A ⊆ B) (h2 : B ⊆ C) : A ⊆ C := subset.trans h1 h2 example : A ⊆ A := subset.refl A example (h : A ⊆ B) : powerset A ⊆ powerset B := sorry example (h : powerset A ⊆ powerset B) : A ⊆ B := sorry
13.关系¶
13. Relations¶
在第 7 章中,我们讨论了一阶逻辑中的关系符号的概念,在第 10 章中,我们了解了如何在模型中解释这种符号。在数学中,我们通常对数学对象之间的不同类型的关系感兴趣,因此关系的概念无处不在。在本章中,我们将考虑一些常见的关系类型。
In Chapter 7 we discussed the notion of a relation symbol in first-order logic, and in Chapter 10 we saw how to interpret such a symbol in a model. In mathematics, we are generally interested in different sorts of relationships between mathematical objects, and so the notion of a relation is ubiquitous. In this chapter, we will consider some common kinds of relations.
在一些公理基础中,关系的概念被认为是原始的,但在公理集合论中,关系被认为是一组具有相应元数的元组。例如,我们可以取二元关系
In some axiomatic foundations, the notion of a relation is taken to be primitive, but in axiomatic set theory, a relation is taken to be a set of tuples of the corresponding arity. For example, we can take a binary relation on
13.1.顺序关系¶
13.1. Order Relations¶
我们将从数学中一类重要的二元关系开始,即偏序。
We will start with a class of important binary relations in mathematics, namely, partial orders.
定义。二元关系
Definition. A binary relation
反身性:
,对于每一个 在reflexivity:
, for every in传递性:如果
和 , 然后 ,对于每一个 , , 和 在transitivity: if
and , then , for every , , and in反对称性:若
和 然后 ,对于每一个 和 在antisymmetry: if
and then , for every and in
注意引入符号的紧凑方式
Notice the compact way of introducing the symbol
不过,现在您已经了解了足够的知识,可以识别这三个子句中存在的全称量词。在符号逻辑中,我们将其写成如下形式:
You now know enough, however, to recognize the universal quantifiers that are present in the three clauses. In symbolic logic, we would write them as follows:
这里的变量
Here the variables
符号的使用
The use of the symbol
关于自然数 on the natural numbers 关于整数 on the integers 关于有理数 on the rational numbers 实数 on the real numbers
但请记住
But keep in mind that
这些并不能完全代表偏序类,因为它们都具有一个附加属性:
These are not fully representative of the class of partial orders, in that they all have an additional property:
定义。偏序
Definition. A partial order
对于每一个
和 在 , 任何一个 或者for every
and in , either or
您可以检查这些是部分顺序而不是全顺序的两个示例:
You can check these these are two examples of partial orders that are not total orders:
分工关系,
,整数the divides relation,
, on the integers子集关系,
在某些域的元素集上the subset relation,
, on sets of elements of some domain
对于整数,我们也有严格的顺序关系,
On the integers, we also have the strict order relation,
定义。二元关系
Definition. A binary relation
非反身性:
对于每一个 在irreflexivity:
for every in传递性:
和 暗示 ,对于每一个 , , 和 在transitivity:
and implies , for every , , and in
如果还具有以下属性,则严格偏序是严格全序(或严格线性序):
A strict partial order is a strict total order (or strict linear order) if, in addition, we have the following property:
三分法:
, , 或者 对于每一个 和 在trichotomy:
, , or for every and in
这里,
Here,
命题。严格偏序
Proposition. A strict partial order
证明。假设
Proof. Suppose
在整数上,
On the integers, there are precise relationships between
定理。假设
Theorem. Suppose
定理。假设
Theorem. Suppose
我们将在这里证明第一个,将第二个留作练习。这个证明很好地说明了全称量化、等式和命题推理是如何在数学论证中结合起来的。
We will prove the first here, and leave the second as an exercise. This proof is a nice illustration of how universal quantification, equality, and propositional reasoning are combined in a mathematical argument.
证明。假设
Proof. Suppose
为了证明传递性,假设
To show transitivity, suppose
为了建立定理中的最后一个断言,假设
To establish the last claim in the theorem, suppose
13.2.有关排序的更多信息¶
13.2. More on Orderings¶
让
Let
我们将从此采用这一惯例。给定一个部分顺序
We will henceforth adopt this convention. Given a partial order
考虑具有小于或等于关系的自然数。它有一个最小元素,
Consider the natural numbers with the less-than-or-equal relation. It has a least element,
小于或等于每个自然数。 is less than or equal to every natural number.没有小于的自然数
。There is no natural number that is less than
.
在符号逻辑中,我们可以将这些语句形式化如下:
In symbolic logic, we could formalize these statements as follows:
使用存在量词,我们可以更忠实地呈现第二条语句,如下所示:
Using the existential quantifier, we could render the second statement more faithfully as follows:
请注意,这个更忠实的陈述与原始陈述等同,使用德摩根定律作为量词。
Notice that this more faithful statement is equivalent to the original, using deMorgan’s laws for quantifiers.
上述两个陈述是否等价?假设一个元素
Are the two statements above equivalent? Say an element
定理。任何最小元素都是最小的。
Theorem. Any minimum element is minimal.
证明。假设
Proof. Suppose
定理。如果偏序
Theorem. If a partial order
证明。假设
Proof. Suppose
请注意,我们将第二定理解释为:如果
Notice that we have interpreted the second theorem as the statement that if
第一定理的逆定理(即每个最小元素都是最小的陈述)是错误的。例如,考虑集合的非空子集
The converse to the first theorem – that is, the statement that every minimal element is minimum – is false. As an example, consider the nonempty subsets of the set
请注意,“偏序的最小元素不一定是最小的”这一陈述做出了一个“存在性”断言:它表示存在一个偏序
Notice that the statement “a minimal element of a partial order is not necessarily minimum” makes an “existential” assertion: it says that there is a partial order
断言存在一个域
The assertion that there exists a domain
我们可以考虑序的其他属性。如果任意两个不同元素之间有另一个元素,则称该序为稠密序。更准确地说,如果
We can consider other properties of orders. An order is said to be dense if between any two distinct elements, there is another element. More precisely, an order is dense if, whenever
13.3.等价关系和相等性¶
13.3. Equivalence Relations and Equality¶
在普通的数学语言中,等价关系定义如下。
In ordinary mathematical language, an equivalence relation is defined as follows.
定义。二元关系
Definition. A binary relation
反身性:
,对于每一个 在reflexivity:
, for every in对称性:如果
, 然后 ,对于每一个 和 在symmetry: if
, then , for every and in传递性:如果
和 , 然后 ,对于每一个 , , 和 在transitivity: if
and , then , for every , , and in
我们让你思考如何用一阶逻辑编写这些语句。(请注意与偏序规则的相似性。)我们还将给你留下一个练习:通过仔细选择如何实例化量词,你实际上可以从以下两个属性中证明上述三个属性:
We leave it to you to think about how you could write these statements in first-order logic. (Note the similarity to the rules for a partial order.) We will also leave you with an exercise: by a careful choice of how to instantiate the quantifiers, you can actually prove the three properties above from the following two:
尝试使用自然演绎或精益来验证这一点。
Try to verify this using natural deduction or Lean.
仅凭这三个属性还不足以表征相等性。您应该检查以下非正式示例是否都是等价关系的实例:
These three properties alone are not strong enough to characterize equality. You should check that the following informal examples are all instances of equivalence relations:
日历上的日子关系为“
和 都落在一周的同一天”the relation on days on the calendar, given by “
and fall on the same day of the week”目前地球上活着的人们之间的关系,由“
和 年龄相同”the relation on people currently alive on the planet, given by “
and have the same age”目前地球上活着的人们之间的关系,由“
和 生日相同”the relation on people currently alive on the planet, given by “
and have the same birthday”美国城市的关系为“
和 处于同一状态”the relation on cities in the United States, given by “
and are in the same state”
以下是两个常见的数学例子:
Here are two common mathematical examples:
平面上直线的关系,由“
和 是平行的”the relation on lines in a plane, given by “
and are parallel”对于任何固定的自然数
,自然数上的关系,由“ 符合于 模数 ”(见第19章)for any fixed natural number
, the relation on natural numbers, given by “ is congruent to modulo ” (see Chapter 19)
在这里,我们说
Here, we say that
考虑美国公民的等价关系,如下“
Consider the equivalence relation on citizens of the United States, given by “
让
Let
动机如下。等价试图捕捉一个弱的平等概念:如果两个元素
The motivation is as follows. Equivalence tries to capture a weak notion of equality: if two elements of
13.4.练习¶
13.4. Exercises¶
认为
是域上的严格偏序 ,并定义 意思是 或者 。表明
是偏序的。表明如果
而且是严格全序,那么 是一个全序。
(上面我们证明了从另一个方向看类似的定理。)
Suppose
is a strict partial order on a domain , and define to mean that or .Show that
is a partial order.Show that if
is moreover a strict total order, then is a total order.
(Above we proved the analogous theorem going in the other direction.)
认为
是域上的严格偏序 。(换句话说,它是传递的和不对称的。)假设 定义为 当且仅当 或者 .我们在课堂上看到 是域上的偏序 ,即它是自反的、传递的、反对称的。证明对于每一个
和 在 ,我们有 当且仅当 和 ,使用上述事实。Suppose
is a strict partial order on a domain . (In other words, it is transitive and asymmetric.) Suppose that is defined so that if and only if or . We saw in class that is a partial order on a domain , i.e.~it is reflexive, transitive, and antisymmetric.Prove that for every
and in , we have iff and , using the facts above.有序图是顶点(点)的集合,以及顶点之间的箭头集合。对于每对顶点,它们之间最多有一个箭头:换句话说,每对顶点要么不相连,要么一个顶点“指向”另一个顶点。请注意,可以从一个顶点指向自身有一个箭头。
定义关系
在顶点集上,对于两个顶点 和 , 表示有一支来自 指向 。在任意图上,
偏序、严格偏序、全序、严格全序,还是以上都不是?如果可能,请给出以下图表的例子: 不具备这些属性。An ordered graph is a collection of vertices (points), along with a collection of arrows between vertices. For each pair of vertices, there is at most one arrow between them: in other words, every pair of vertices is either unconnected, or one vertex is “directed” toward the other. Note that it is possible to have an arrow from a vertex to itself.
Define a relation
on the set of vertices, such that for two vertices and , means that there is an arrow from pointing to .On an arbitrary graph, is
a partial order, a strict partial order, a total order, a strict total order, or none of the above? If possible, give examples of graphs where fails to have these properties.让
是集合上的等价关系 . 对于每个元素 在 , 让 是等价类 :即元素集合 . 证明对于每一个 和 , 当且仅当 。(提示和说明:
请记住,由于您正在证明“当且仅当”语句,因此有两个方向需要证明。
自此以后
和 是集合, 意味着对于每个元素 , 位于 当且仅当 位于 。根据定义,元素
位于 当且仅当 。 尤其, 位于 。
Let
be an equivalence relation on a set . For every element in , let be the equivalence class of : that is, the set of elements . Show that for every and , if and only if .(Hints and notes:
Remember that since you are proving an ``if and only if’’ statement, there are two directions to prove.
Since that
and are sets, means that for every element , is in if and only if is in .By definition, an element
is in if and only if . In particular, is in .)
让关系
关于自然数 定义如下:如果 是偶数,那么 ,如果 是奇数,那么 。此外,对于每一个 , . 表明 是等价关系。数字 5 的等价类是什么?描述等价类的集合 。Let the relation
on the natural numbers be defined as follows: if is even, then , and if is odd, then . Furthermore, for every , . Show that is an equivalence relation. What is the equivalence class of the number 5? Describe the set of equivalence classes .证明平面上直线的关系由“
和 是平行的”,是等价关系。x 轴的等价类是什么?描述等价类的集合 。Show that the relation on lines in the plane, given by “
and are parallel,” is an equivalence relation. What is the equivalence class of the x-axis? Describe the set of equivalence classes .二元关系
在域上 被称为预序,它是自反的和传递的。这比说它是偏序要弱;我们删除了关系不对称的要求。一个例子是地球上目前活着的人的排序,其定义为 当且仅当 出生日期早于 's。不对称性不成立,因为不同的人可以在同一天出生。但是,证明以下定理成立:定理。设
成为域名预订单 . 定义关系 , 在哪里 当且仅当 和 。 然后 是上的等价关系 。A binary relation
on a domain is said to be a preorder it is is reflexive and transitive. This is weaker than saying it is a partial order; we have removed the requirement that the relation is asymmetric. An example is the ordering on people currently alive on the planet defined by setting if and only if ‘s birth date is earlier than ‘s. Asymmetry fails, because different people can be born on the same day. But, prove that the following theorem holds:Theorem. Let
be a preorder on a domain . Define the relation , where holds if and only if and . Then is an equivalence relation on .
14.精益中的关系¶
14. Relations in Lean¶
在上一章中,我们注意到集合论者认为二元关系A是函数。请记住,箭头向右关联,因此实际上意味着。因此,给定是一个谓词(与 相关的属性) ,而给定是一个命题。A → A → PropA → A → PropA → (A → Prop)a : AR aAa b : AR a b
In the last chapter, we noted that set theorists think of a binary relation A is a function A → A → Prop. Remember that the arrows associate to the right, so A → A → Prop really means A → (A → Prop). So, given a : A, R a is a predicate (the property of being related to A), and given a b : A, R a b is a proposition.
14.1.顺序关系¶
14.1. Order Relations¶
利用一阶逻辑,我们可以说出关系的自反性、对称性、传递性、反对称性等等的含义:
With first-order logic, we can say what it means for a relation to be reflexive, symmetric, transitive, antisymmetric, and so on:
namespace hidden
variable {A : Type}
def reflexive (R : A → A → Prop) : Prop :=
∀ x, R x x
def symmetric (R : A → A → Prop) : Prop :=
∀ x y, R x y → R y x
def transitive (R : A → A → Prop) : Prop :=
∀ x y z, R x y → R y z → R x z
def anti_symmetric (R : A → A → Prop) : Prop :=
∀ x y, R x y → R y x → x = y
end hidden
namespace hidden
variable {A : Type}
def reflexive (R : A → A → Prop) : Prop :=
∀ x, R x x
def symmetric (R : A → A → Prop) : Prop :=
∀ x y, R x y → R y x
def transitive (R : A → A → Prop) : Prop :=
∀ x y z, R x y → R y z → R x z
def anti_symmetric (R : A → A → Prop) : Prop :=
∀ x y, R x y → R y x → x = y
end hidden
然后我们就可以自由使用这些概念了。请注意,精益会在必要时展开定义,例如,将视为。reflexive R∀ x, R x x
We can then use the notions freely. Notice that Lean will unfold the definitions when necessary, for example, treating reflexive R as ∀ x, R x x.
variable R : A → A → Prop
example (h : reflexive R) (x : A) : R x x := h x
example (h : symmetric R) (x y : A) (h1 : R x y) : R y x :=
h x y h1
example (h : transitive R) (x y z : A) (h1 : R x y) (h2 : R y z) :
R x z :=
h x y z h1 h2
example (h : anti_symmetric R) (x y : A) (h1 : R x y)
(h2 : R y x) :
x = y :=
h x y h1 h2
variable R : A → A → Prop
example (h : reflexive R) (x : A) : R x x := h x
example (h : symmetric R) (x y : A) (h1 : R x y) : R y x :=
h x y h1
example (h : transitive R) (x y z : A) (h1 : R x y) (h2 : R y z) :
R x z :=
h x y z h1 h2
example (h : anti_symmetric R) (x y : A) (h1 : R x y)
(h2 : R y x) :
x = y :=
h x y h1 h2
在命令 中,我们用花括号括起来,表示它是一个隐式参数,也就是说,您不必明确地写出它;Lean 可以从参数 中推断出它。这就是为什么我们可以写而不是:Lean 知道是 上的二元关系,所以它可以推断出我们指的是 上的二元关系的反身性。variable {A : Type}ARreflexive Rreflexive A RRAA
In the command variable {A : Type}, we put curly braces around A to indicate that it is an implicit argument, which is to say, you do not have to write it explicitly; Lean can infer it from the argument R. That is why we can write reflexive R rather than reflexive A R: Lean knows that R is a binary relation on A, so it can infer that we mean reflexivity for binary relations on A.
给定、和,写出来证明很烦人。毕竟,Lean 应该能够从 和 的事实推断出我们在 、 和 处讨论的是传递性。事实上,我们可以用下划线代替该信息:h : transitive Rh1 : R x yh2 : R y zh x y z h1 h2R x zxyzh1R x yh2R y z
Given h : transitive R, h1 : R x y, and h2 : R y z, it is annoying to have to write h x y z h1 h2 to prove R x z. After all, Lean should be able to infer that we are talking about transitivity at x, y, and z, from the fact that h1 is R x y and h2 is R y z. Indeed, we can replace that information by underscores:
variable R : A → A → Prop
example (h : transitive R) (x y z : A) (h1 : R x y)
(h2 : R y z) :
R x z :=
h _ _ _ h1 h2
variable R : A → A → Prop
example (h : transitive R) (x y z : A) (h1 : R x y)
(h2 : R y z) :
R x z :=
h _ _ _ h1 h2
但输入下划线也很烦人。最好的解决方案是将传递性假设的参数也声明为隐式的:x y z
But typing underscores is annoying, too. The best solution is to declare the arguments x y z to a transitivity hypothesis to be implicit as well:
variable {A : Type}
variable R : A → A → Prop
example (h : transitive R) (x y z : A) (h1 : R x y) (h2 : R y z) :
R x z :=
h h1 h2
variable {A : Type}
variable R : A → A → Prop
example (h : transitive R) (x y z : A) (h1 : R x y) (h2 : R y z) :
R x z :=
h h1 h2
事实上, Lean 核心库中的概念reflexive、symmetric、transitive和正是以这种方式定义的,因此我们可以自由使用它们而无需定义它们。这就是为什么我们将 的临时定义放在命名空间 中的原因;这意味着我们版本的 的全名是,因此,它与库中定义的名称不冲突。anti_symmetrichiddenreflexivehidden.reflexive
In fact, the notions reflexive, symmetric, transitive, and anti_symmetric are defined in Lean’s core library in exactly this way, so we are free to use them without defining them. That is why we put our temporary definitions of in a namespace hidden; that means that the full name of our version of reflexive is hidden.reflexive, which, therefore, doesn’t conflict with the one defined in the library.
在第 13.1 节中,我们展示了严格偏序(即传递和非自反的二元关系)也是不对称的。以下是 Lean 中对该事实的证明。
In Section 13.1 we showed that a strict partial order—that is, a binary relation that is transitive and irreflexive—is also asymmetric. Here is a proof of that fact in Lean.
variable A : Type
variable R : A → A → Prop
example (h1 : irreflexive R) (h2 : transitive R) :
∀ x y, R x y → ¬ R y x :=
assume x y,
assume h3 : R x y,
assume h4 : R y x,
have h5 : R x x, from h2 h3 h4,
have h6 : ¬ R x x, from h1 x,
show false, from h6 h5
variable A : Type
variable R : A → A → Prop
example (h1 : irreflexive R) (h2 : transitive R) :
∀ x y, R x y → ¬ R y x :=
assume x y,
assume h3 : R x y,
assume h4 : R y x,
have h5 : R x x, from h2 h3 h4,
have h6 : ¬ R x x, from h1 x,
show false, from h6 h5
在数学中,通常使用中缀符号和符号≤来表示偏序。 Lean 支持这种做法:
In mathematics, it is common to use infix notation and a symbol like ≤ to denote a partial order. Lean supports this practice:
section
parameter A : Type
parameter R : A → A → Prop
local infix < := R
example (h1 : irreflexive R) (h2 : transitive R) :
∀ x y, x < y → ¬ y < x :=
assume x y,
assume h3 : x < y,
assume h4 : y < x,
have h5 : x < x, from h2 h3 h4,
have h6 : ¬ x < x, from h1 x,
show false, from h6 h5
end
section
parameter A : Type
parameter R : A → A → Prop
local infix < := R
example (h1 : irreflexive R) (h2 : transitive R) :
∀ x y, x < y → ¬ y < x :=
assume x y,
assume h3 : x < y,
assume h4 : y < x,
have h5 : x < x, from h2 h3 h4,
have h6 : ¬ x < x, from h1 x,
show false, from h6 h5
end
parameter和命令与和命令parameters类似,不同之处在于参数在节内是固定的。换句话说,如果您在上面的节中证明了关于 的定理,则您不能将该定理应用于另一个关系 ,除非关闭该节。由于参数是固定的,Lean 允许我们定义要在节中本地使用的 的符号。variablevariablesRSRR
The parameter and parameters commands are similar to the variable and variables commands, except that parameters are fixed within a section. In other words, if you prove a theorem about R in the section above, you cannot apply that theorem to another relation, S, without closing the section. Since the parameter R is fixed, Lean allows us to define notation for R to be used locally in the section.
在下面的例子中,固定了偏序,R我们定义相应的严格偏序,并证明它确实是一个严格序。
In the example below, having fixed a partial order, R, we define the corresponding strict partial order and prove that it is, indeed, a strict order.
section
parameters {A : Type} (R : A → A → Prop)
parameter (reflR : reflexive R)
parameter (transR : transitive R)
parameter (antisymmR : ∀ {a b : A}, R a b → R b a → a = b)
local infix ≤ := R
definition R' (a b : A) : Prop := a ≤ b ∧ a ≠ b
local infix < := R'
theorem irreflR (a : A) : ¬ a < a :=
assume : a < a,
have a ≠ a, from and.right this,
have a = a, from rfl,
show false, from ‹a ≠ a› ‹a = a›
theorem transR {a b c : A} (h₁ : a < b) (h₂ : b < c) : a < c :=
have a ≤ b, from and.left h₁,
have a ≠ b, from and.right h₁,
have b ≤ c, from and.left h₂,
have b ≠ c, from and.right h₂,
have a ≤ c, from transR ‹a ≤ b› ‹b ≤ c›,
have a ≠ c, from
assume : a = c,
have c ≤ b, from eq.subst ‹a = c› ‹a ≤ b›,
have b = c, from antisymmR ‹b ≤ c› ‹c ≤ b›,
show false, from ‹b ≠ c› ‹b = c›,
show a < c, from and.intro ‹a ≤ c› ‹a ≠ c›
end
section
parameters {A : Type} (R : A → A → Prop)
parameter (reflR : reflexive R)
parameter (transR : transitive R)
parameter (antisymmR : ∀ {a b : A}, R a b → R b a → a = b)
local infix ≤ := R
definition R' (a b : A) : Prop := a ≤ b ∧ a ≠ b
local infix < := R'
theorem irreflR (a : A) : ¬ a < a :=
assume : a < a,
have a ≠ a, from and.right this,
have a = a, from rfl,
show false, from ‹a ≠ a› ‹a = a›
theorem transR {a b c : A} (h₁ : a < b) (h₂ : b < c) : a < c :=
have a ≤ b, from and.left h₁,
have a ≠ b, from and.right h₁,
have b ≤ c, from and.left h₂,
have b ≠ c, from and.right h₂,
have a ≤ c, from transR ‹a ≤ b› ‹b ≤ c›,
have a ≠ c, from
assume : a = c,
have c ≤ b, from eq.subst ‹a = c› ‹a ≤ b›,
have b = c, from antisymmR ‹b ≤ c› ‹c ≤ b›,
show false, from ‹b ≠ c› ‹b = c›,
show a < c, from and.intro ‹a ≤ c› ‹a ≠ c›
end
请注意,我们使用了暗示性名称reflR,transR,antisymmR而不是h1,h2,h3以帮助记住哪个假设是哪个假设。证明还使用了匿名have和,并使用法语引号anfassume引用它们。还要记住,这是事实的证明,相当于中的。您还可以使用等效符号,其中三角形写为。\f<\f>eq.subst ‹a = c› ‹a ≤ b›caa ≤ b‹a = c› ▸ ‹a ≤ b›\t
Notice that we have used suggestive names reflR, transR, antisymmR instead of h1, h2, h3 to help remember which hypothesis is which. The proof also uses anonymous have and assume, referring back to them with the French quotes, \f< anf \f>. Remember also that eq.subst ‹a = c› ‹a ≤ b› is a proof of the fact that amounts for substituting c for a in a ≤ b. You can also use the equivalent notation ‹a = c› ▸ ‹a ≤ b›, where the triangle is written \t.
在第 13.1节中,我们还指出,你可以从严格偏序中定义(弱)偏序。我们要求你在下面的练习中正式地做到这一点。
In Section Section 13.1, we also noted that you can define a (weak) partial order from a strict one. We ask you to do this formally in the exercises below.
这里还有一个例子。假设R是类型上的二元关系A,我们定义表示和都成立。下面我们表明结果关系是自反和对称的。S x yR x yR y x
Here is one more example. Suppose R is a binary relation on a type A, and we define S x y to mean that both R x y and R y x holds. Below we show that the resulting relation is reflexive and symmetric.
section
parameter A : Type
parameter R : A → A → Prop
variable h1 : transitive R
variable h2 : reflexive R
def S (x y : A) := R x y ∧ R y x
example : reflexive S :=
assume x,
have R x x, from h2 x,
show S x x, from and.intro this this
example : symmetric S :=
assume x y,
assume h : S x y,
have h1 : R x y, from h.left,
have h2 : R y x, from h.right,
show S y x, from ⟨h.right, h.left⟩
end
section
parameter A : Type
parameter R : A → A → Prop
variable h1 : transitive R
variable h2 : reflexive R
def S (x y : A) := R x y ∧ R y x
example : reflexive S :=
assume x,
have R x x, from h2 x,
show S x x, from and.intro this this
example : symmetric S :=
assume x y,
assume h : S x y,
have h1 : R x y, from h.left,
have h2 : R y x, from h.right,
show S y x, from ⟨h.right, h.left⟩
end
在下面的练习中,我们要求您证明这S也是及物的。
In the exercises below, we ask you to show that S is transitive as well.
在第一个例子中,我们使用匿名的assumeand have,然后使用have关键字引用回this。在第二个例子中,我们分别将and缩写为and 。我们还使用匿名构造函数缩写,写为。Lean 发现我们试图证明一个合取,并发现这是相关的引入原理。您可以分别用和键入角括号。and.left hand.right hh.lefth.rightand.intro h.right h.left⟨h.right, h.left⟩and.intro\<\>
In the first example, we use the anonymous assume and have, and then refer back to the have with the keyword this. In the second example, we abbreviate and.left h and and.right h as h.left and h.right, respectively. We also abbreviate and.intro h.right h.left with an anonymous constructor, writing ⟨h.right, h.left⟩. Lean figures out that we are trying to prove a conjunction, and figures out that and.intro is the relevant introduction principle. You can type the corner brackets with \< and \>, respectively.
14.2.数字排序¶
14.2. Orderings on Numbers¶
方便的是,Lean 已经定义了自然数、整数等的正常排序。
Conveniently, Lean has the normal orderings on the natural numbers, integers, and so on defined already.
open nat
variables n m : ℕ
#check 0 ≤ n
#check n < n + 1
example : 0 ≤ n := nat.zero_le n
example : n < n + 1 := lt_succ_self n
example (h : n + 1 ≤ m) : n < m + 1 :=
have h1 : n < n + 1, from lt_succ_self n,
have h2 : n < m, from lt_of_lt_of_le h1 h,
have h3 : m < m + 1, from lt_succ_self m,
show n < m + 1, from lt_trans h2 h3
open nat
variables n m : ℕ
#check 0 ≤ n
#check n < n + 1
example : 0 ≤ n := nat.zero_le n
example : n < n + 1 := lt_succ_self n
example (h : n + 1 ≤ m) : n < m + 1 :=
have h1 : n < n + 1, from lt_succ_self n,
have h2 : n < m, from lt_of_lt_of_le h1 h,
have h3 : m < m + 1, from lt_succ_self m,
show n < m + 1, from lt_trans h2 h3
Lean 中有许多定理可用于证明不等式关系的事实。我们在此列出一些常见的定理。
There are many theorems in Lean that are useful for proving facts about inequality relations. We list some common ones here.
variables (A : Type) [partial_order A]
variables a b c : A
#check (le_trans : a ≤ b → b ≤ c → a ≤ c)
#check (lt_trans : a < b → b < c → a < c)
#check (lt_of_lt_of_le : a < b → b ≤ c → a < c)
#check (lt_of_le_of_lt : a ≤ b → b < c → a < c)
#check (le_of_lt : a < b → a ≤ b)
variables (A : Type) [partial_order A]
variables a b c : A
#check (le_trans : a ≤ b → b ≤ c → a ≤ c)
#check (lt_trans : a < b → b < c → a < c)
#check (lt_of_lt_of_le : a < b → b ≤ c → a < c)
#check (lt_of_le_of_lt : a ≤ b → b < c → a < c)
#check (le_of_lt : a < b → a ≤ b)
这里顶部的声明表明它A具有偏序结构。还有一些特定于某些域的属性,例如自然数:
Here the declaration at the top says that A has the structure of a partial order. There are also properties that are specific to some domains, like the natural numbers:
variable n : ℕ
#check (nat.zero_le : ∀ n : ℕ, 0 ≤ n)
#check (nat.lt_succ_self : ∀ n : ℕ, n < n + 1)
#check (nat.le_succ : ∀ n : ℕ, n ≤ n + 1)
variable n : ℕ
#check (nat.zero_le : ∀ n : ℕ, 0 ≤ n)
#check (nat.lt_succ_self : ∀ n : ℕ, n < n + 1)
#check (nat.le_succ : ∀ n : ℕ, n ≤ n + 1)
14.3.等价关系¶
14.3. Equivalence Relations¶
在13.3 节中,我们看到等价关系是某个域上的二元关系
In Section 13.3 we saw that an equivalence relation is a binary relation on some domain
namespace hidden
variable {A : Type}
def preorder (R : A → A → Prop) : Prop :=
reflexive R ∧ transitive R
end hidden
namespace hidden
variable {A : Type}
def preorder (R : A → A → Prop) : Prop :=
reflexive R ∧ transitive R
end hidden
Lean 的库提供了不同的预序公式,因此,为了使用相同的名称,我们必须将其放在hidden命名空间中。Lean 的库定义了关系的其他属性,例如:
Lean’s library provides a different formulation of preorders, so, in order to use the same name, we have to put it in the hidden namespace. Lean’s library defines other properties of relations, such as these:
namespace hidden
variables {A : Type} (R : A → A → Prop)
def equivalence := reflexive R ∧ symmetric R ∧ transitive R
def total := ∀ x y, R x y ∨ R y x
def irreflexive := ∀ x, ¬ R x x
def anti_symmetric := ∀ ⦃x y⦄, R x y → R y x → x = y
end hidden
namespace hidden
variables {A : Type} (R : A → A → Prop)
def equivalence := reflexive R ∧ symmetric R ∧ transitive R
def total := ∀ x y, R x y ∨ R y x
def irreflexive := ∀ x, ¬ R x x
def anti_symmetric := ∀ ⦃x y⦄, R x y → R y x → x = y
end hidden
你可以要求 Lean 打印它们的定义:
You can ask Lean to print their definitions:
#print equivalence
#print total
#print irreflexive
#print anti_symmetric
#print equivalence
#print total
#print irreflexive
#print anti_symmetric
基于我们之前对预序的定义,我们可以将偏序描述为反对称预序,并证明等价关系为对称预序。
Building on our previous definition of a preorder, we can describe a partial order as an antisymmetric preorder, and show that an equivalence relation as a symmetric preorder.
namespace hidden
variable {A : Type}
def preorder (R : A → A → Prop) : Prop :=
reflexive R ∧ transitive R
def partial_order (R : A → A → Prop) : Prop :=
preorder R ∧ anti_symmetric R
example (R : A → A → Prop):
equivalence R ↔ preorder R ∧ symmetric R :=
iff.intro
(assume h1 : equivalence R,
have h2 : reflexive R, from and.left h1,
have h3 : symmetric R, from and.left (and.right h1),
have h4 : transitive R, from and.right (and.right h1),
show preorder R ∧ symmetric R,
from and.intro (and.intro h2 h4) h3)
(assume h1 : preorder R ∧ symmetric R,
have h2 : preorder R, from and.left h1,
show equivalence R,
from and.intro (and.left h2)
(and.intro (and.right h1) (and.right h2)))
end hidden
namespace hidden
variable {A : Type}
def preorder (R : A → A → Prop) : Prop :=
reflexive R ∧ transitive R
def partial_order (R : A → A → Prop) : Prop :=
preorder R ∧ anti_symmetric R
example (R : A → A → Prop):
equivalence R ↔ preorder R ∧ symmetric R :=
iff.intro
(assume h1 : equivalence R,
have h2 : reflexive R, from and.left h1,
have h3 : symmetric R, from and.left (and.right h1),
have h4 : transitive R, from and.right (and.right h1),
show preorder R ∧ symmetric R,
from and.intro (and.intro h2 h4) h3)
(assume h1 : preorder R ∧ symmetric R,
have h2 : preorder R, from and.left h1,
show equivalence R,
from and.intro (and.left h2)
(and.intro (and.right h1) (and.right h2)))
end hidden
在第 13.3 节中,我们声称还有另一种定义等价关系的方法,即满足以下两个属性的二元关系:
In Section 13.3 we claimed that there is yet another way to define an equivalence relation, namely, as a binary relation satisfying the following two properties:
让我们在 Lean 中证明这一点。请记住,parameters和命令用于修复关系并引入符号来表示它。(您可以键入为。)在假设和中,符号表示。local infixR≈≈\~~reflexive (≈)symmetric (≈)(≈)R
Let’s prove this in Lean. Remember that the parameters and local infix commands serve to fix a relation R and introduce the notation ≈ to denote it. (You can type ≈ as \~~.) In the assumptions reflexive (≈) and symmetric (≈), the notation (≈) denotes R.
section
parameters {A : Type} (R : A → A → Prop)
local infix ≈ := R
variable (h1 : reflexive (≈))
variable (h2 : ∀ {a b c}, a ≈ b ∧ c ≈ b → a ≈ c)
example : symmetric (≈) :=
assume a b (h : a ≈ b),
have b ≈ b ∧ a ≈ b, from and.intro (h1 b) h,
show b ≈ a, from h2 this
example : transitive (≈) :=
assume a b c (h3 : a ≈ b) (h4 : b ≈ c),
have c ≈ b, from h2 (and.intro (h1 c) h4),
have a ≈ b ∧ c ≈ b, from and.intro h3 this,
show a ≈ c, from h2 this
end
section
parameters {A : Type} (R : A → A → Prop)
local infix ≈ := R
variable (h1 : reflexive (≈))
variable (h2 : ∀ {a b c}, a ≈ b ∧ c ≈ b → a ≈ c)
example : symmetric (≈) :=
assume a b (h : a ≈ b),
have b ≈ b ∧ a ≈ b, from and.intro (h1 b) h,
show b ≈ a, from h2 this
example : transitive (≈) :=
assume a b c (h3 : a ≈ b) (h4 : b ≈ c),
have c ≈ b, from h2 (and.intro (h1 c) h4),
have a ≈ b ∧ c ≈ b, from and.intro h3 this,
show a ≈ c, from h2 this
end
14.4.练习¶
14.4. Exercises¶
替换
sorry以下证明中的命令,以表明我们可以R'从严格偏序中创建偏序R。section parameters {A : Type} {R : A → A → Prop} parameter (irreflR : irreflexive R) parameter (transR : transitive R) local infix < := R def R' (a b : A) : Prop := R a b ∨ a = b local infix ≤ := R' theorem reflR' (a : A) : a ≤ a := sorry theorem transR' {a b c : A} (h1 : a ≤ b) (h2 : b ≤ c): a ≤ c := sorry theorem antisymmR' {a b : A} (h1 : a ≤ b) (h2 : b ≤ a) : a = b := sorry end
Replace the
sorrycommands in the following proofs to show that we can create a partial orderR'out of a strict partial orderR.section parameters {A : Type} {R : A → A → Prop} parameter (irreflR : irreflexive R) parameter (transR : transitive R) local infix < := R def R' (a b : A) : Prop := R a b ∨ a = b local infix ≤ := R' theorem reflR' (a : A) : a ≤ a := sorry theorem transR' {a b c : A} (h1 : a ≤ b) (h2 : b ≤ c): a ≤ c := sorry theorem antisymmR' {a b : A} (h1 : a ≤ b) (h2 : b ≤ a) : a = b := sorry end
sorry用证明代替。section parameters {A : Type} {R : A → A → Prop} parameter (reflR : reflexive R) parameter (transR : transitive R) def S (a b : A) : Prop := R a b ∧ R b a example : transitive S := sorry end
Replace the
sorryby a proof.section parameters {A : Type} {R : A → A → Prop} parameter (reflR : reflexive R) parameter (transR : transitive R) def S (a b : A) : Prop := R a b ∧ R b a example : transitive S := sorry end
以下两个定理中只有一个是可证明的。找出哪一个是真的,并
sorry用完整的证明替换命令。section parameters {A : Type} {a b c : A} {R : A → A → Prop} parameter (Rab : R a b) parameter (Rbc : R b c) parameter (nRac : ¬ R a c) -- Prove one of the following two theorems: theorem R_is_strict_partial_order : irreflexive R ∧ transitive R := sorry theorem R_is_not_strict_partial_order : ¬(irreflexive R ∧ transitive R) := sorry end
Only one of the following two theorems is provable. Figure out which one is true, and replace the
sorrycommand with a complete proof.section parameters {A : Type} {a b c : A} {R : A → A → Prop} parameter (Rab : R a b) parameter (Rbc : R b c) parameter (nRac : ¬ R a c) -- Prove one of the following two theorems: theorem R_is_strict_partial_order : irreflexive R ∧ transitive R := sorry theorem R_is_not_strict_partial_order : ¬(irreflexive R ∧ transitive R) := sorry end
完成下列证明。
open nat example : 1 ≤ 4 := sorry
Complete the following proof.
open nat example : 1 ≤ 4 := sorry
15.函数¶
15. Functions¶
十九世纪末,数学许多分支学科的发展推动了集合、函数和关系的统一处理。我们已经讨论了集合和关系。在本章中,我们将讨论函数及其属性。
In the late nineteenth century, developments in a number of branches of mathematics pushed towards a uniform treatment of sets, functions, and relations. We have already considered sets and relations. In this chapter, we consider functions and their properties.
一个函数,X元素类型和一组A该类型的元素。因此,在类型理论公式中,考虑类型X和之间的函数是很自然的,并考虑它们相对于和Y的子集的行为。XY
A function, X of elements, and a set A of elements of that type. Thus, in the type-theoretic formulation, it is natural to consider functions between types X and Y, and consider their behavior with respect to subsets of X and Y.
然而,在日常数学中,集合论语言很常见,大多数数学家认为函数是集合之间的映射。因此,当从数学角度讨论函数时,我们也将采用这种语言,稍后在讨论精益中的形式化时将切换到类型论表示。
In everyday mathematics, however, set-theoretic language is common, and most mathematicians think of a function as a map between sets. When discussing functions from a mathematical standpoint, therefore, we will also adopt this language, and later switch to the type-theoretic representation when we talk about formalization in Lean.
15.1.函数概念¶
15.1. The Function Concept¶
如果
If
定义函数最简单的方法是在每次
The simplest way to define a function is to give its value at every
让
定义为函数 。Let
be the function defined by .让
定义为函数 。Let
be the function defined by .让
定义为函数 。Let
be the function defined by .让
定义为函数Let
be the function defined by
使用显式表达式定义函数的能力提出了一个基本问题,即什么才算合法的“表达式”。现在,让我们把这个问题放在一边,只需注意现代数学对各种奇特的定义都很熟悉。例如,我们可以定义一个函数
The ability to define functions using an explicit expression raises the foundational question as to what counts as legitimate “expression.” For the moment, let us set that question aside, and simply note that modern mathematics is comfortable with all kinds of exotic definitions. For example, we can define a function
这与函数在某种意义上是可计算对象的观点相矛盾。将实数作为输入意味着什么根本不清楚,更不用说是否有可能通过算法确定这样的数字是否合理。我们将在后面的章节中回顾这些问题。
This is at odds with a view of functions as objects that are computable in some sense. It is not at all clear what it means to be presented with a real number as input, let alone whether it is possible to determine, algorithmically, whether such a number is rational or not. We will return to such issues in a later chapter.
注意变量的选择
Notice that the choice of the variables
对于任意集合
For any set
我们想到两个函数
We think of two functions
命题。对于每一个
Proposition. For every
证明。设
Proof. Let
认为
Suppose
定义
经过 和 。 然后 是 ,反之亦然。Define
by and . Then is both a left and a right inverse to , and vice-versa.写
表示非负实数。定义 经过 ,并定义 经过 。 然后 对于每一个 在...的领域 , 所以 是 , 和 是 。 另一方面, ,这与 什么时候 为负。所以 不是左逆 , 和 不是右逆 。Write
to denote the nonnegative reals. Define by , and define by . Then for every in the domain of , so is a left inverse to , and is a right inverse to . On the other hand, , which is not the same as when is negative. So is not a left inverse to , and is not a right inverse to .
尽管证明很简短,但下列事实却并不明显:
The following fact is not at all obvious, even though the proof is short:
命题。假设
Proposition. Suppose
证明。设
Proof. Let
如果
If
命题。假设
Proposition. Suppose
证明。这可从前面的命题得出,因为(比如说)
Proof. This follows from the previous proposition, since (say)
什么时候
When
命题。假设
Proposition. Suppose
证明。对于每一个
Proof. For every
推论:将“左”替换为“右”,上一个命题仍然成立。
Corollary. The previous proposition holds with “left” replaced by “right.”
证明。转换角色
Proof. Switch the role of
推论。如果
Corollary. If
15.2.单射、全射和双射函数¶
15.2. Injective, Surjective, and Bijective Functions¶
函数
A function
函数
A function
函数
A function
接下来的两个命题表明,这些概念可以根据逆的存在来表述。
The next two propositions show that these notions can be cast in terms of the existence of inverses.
命题。设
Proposition. Let
如果
有一个左逆,那么 是单射。If
has a left inverse, then is injective.如果
有一个右逆,那么 是全射。If
has a right inverse, then is surjective.如果
有一个逆,那么它就是 双射的。If
has an inverse, then it is bijective.
证明。对于第一个断言,假设
Proof. For the first claim, suppose
对于第二个主张,假设
For the second claim, suppose
第三个主张是根据前两个主张得出的。
The third claim follows from the first two.
以下命题更有趣,因为它要求我们定义新函数,给出以下假设
The following proposition is more interesting, because it requires us to define new functions, given hypotheses on
命题。设
Proposition. Let
如果
不为空,并且 是单射,那么 有一个左逆。If
is nonempty and is injective, then has a left inverse.如果
是全射,那么 有一个右逆。If
is surjective, then has a right inverse.如果
如果是双射,那么它有一个逆。If
if bijective, then it has an inverse.
证明。对于第一个断言,让
Proof. For the first claim, let
对于第二项主张,因为
For the second claim, because
请注意
Notice that the definition of
利用这些等价性和上一节的结果,我们可以证明以下内容:
Using these equivalences and the results in the previous section, we can prove the following:
命题。设
Proposition. Let
如果
和 是单射,那么也是 。If
and are injective, then so is .如果
和 是全射,那么 。If
and are surjective, then so is .
证明。如果
Proof. If
然而,我们可以证明这两个命题,而根本不需要提及逆命题。我们把这个留给你作为练习。
We can prove these two statements, however, without mentioning inverses at all. We leave that to you as an exercise.
请注意表达式
Notice that the expression
函数
a function
函数
a function
函数
a function
只有第三个函数是全射。因此,函数的余域和定义域的指定对于判断函数是否为全射至关重要。
Only the third one is surjective. Thus a specification of the function’s codomain as well as the domain is essential to making sense of whether a function is surjective.
15.3.域的函数和子集¶
15.3. Functions and Subsets of the Domain¶
认为
Suppose
如果
If
换句话说,
In words,
命题。假设
Proposition. Suppose
证明。根据定义,
Proof. By definition,
命题。假设
Proposition. Suppose
证明。假设
Proof. Suppose
相反,假设
Conversely, suppose
请注意,如果
Notice that if
认为
Suppose
函数还有另一个重要的运算,称为原像。如果
There is another important operation on functions, known as the preimage. If
即元素集
that is, the set of elements of
命题。假设
Proposition. Suppose
证明。对于任何
Proof. For any
这里我们给出了图像和原像的一系列事实属性。这里,
Here we give a long list of facts properties of images and preimages. Here,
,如果 是单射, 。 , and if is injective, . ,如果 是全射, 。 , and if is surjective, .如果
, 然后 。If
, then .如果
, 然后 。If
, then . 。 . 。 . ,如果 是单射, 。 , and if is injective, . 。 . 。 . 。 . 。 . 。 . 。 . 。 .
证明这样的恒等式通常需要展开定义并使用基本的逻辑推理。以下是一个例子。
Proving identities like this is typically a matter of unfolding definitions and using basic logical inferences. Here is an example.
命题。设
Proposition. Let
证明。假设
Proof. Suppose
相反,假设
Conversely, suppose
15.4.函数和关系¶
15.4. Functions and Relations¶
二元关系
A binary relation
不难检验函数和关系是成对出现的:如果
It is not hard to check that functions and relations travel in pairs: if
到目前为止,我们一直关注于接受单个参数的函数。我们也可以考虑函数
So far, we have been focusing on functions that take a single argument. We can also consider functions
稍后,当我们考虑集合论和类型论基础时,我们将回到这些对更高元数函数进行建模的不同方法。我们再次指出,在普通数学中,基础细节并不重要。上述两种选择是可相互翻译的,并且认可对函数进行非形式化推理的相同原则。
We will return to these different ways of modeling functions of higher arity later on, when we consider set-theoretic and type-theoretic foundations. One again, we remark that in ordinary mathematics, the foundational details do not matter much. The two choices above are inter-translatable, and sanction the same principles for reasoning about functions informally.
在数学中,我们经常考虑偏函数的概念
In mathematics, we often also consider the notion of a partial function from
就关系而言,偏函数
In terms of relations, a partial function
15.5.练习¶
15.5. Exercises¶
让
是来自的任何函数 到 ,并让 是来自的任何函数 到 。表明如果
是单射,那么 是单射。举一个函数的例子
和 如上所述,这样 是单射,但是 不是单射。表明如果
是单射,并且 是全射,那么 是单射。
Let
be any function from to , and let be any function from to .Show that if
is injective, then is injective.Give an example of functions
and as above, such that that is injective, but is not injective.Show that if
is injective and is surjective, then is injective.
让
和 和上一个问题一样。假设 是全射。是
一定是全射吗?要么证明它是全射,要么给出反例。是
一定是全射吗?要么证明它是全射,要么给出反例。
Let
and be as in the last problem. Suppose is surjective.Is
necessarily surjective? Either prove that it is, or give a counterexample.Is
necessarily surjective? Either prove that it is, or give a counterexample.
函数
从 到 被称为 严格增加,如果 , 。表明如果
是严格递增的,那么它是单射(因此它有一个左逆)。表明如果
严格增加,并且 是 , 然后 是严格递增的。
A function
from to is said to be strictly increasing if whenever , .Show that if
is strictly increasing, then it is injective (and hence it has a left inverse).Show that if
is strictly increasing, and is a right inverse to , then is strictly increasing.
让
是任意函数,设 和 是子集 . 表明 。Let
be any function, and let and be subsets of . Show that .让
是任意函数,设 和 是任意子集 。 展示 。Let
be any function, and let and be any subsets of . Show .定义二元关系的组合和逆的概念,以概括函数的概念。
Define notions of composition and inverse for binary relations that generalize the notions for functions.
16. Lean 中的函数¶
16. Functions in Lean¶
16.1.函数和符号逻辑¶
16.1. Functions and Symbolic Logic¶
现在让我们从形式上来考虑函数。尽管我们在上一章的定义中避免使用量词和逻辑符号,但现在你应该已经看到它们潜伏在表面之下。事实上,两个函数
Let us now consider functions in formal terms. Even though we have avoided the use of quantifiers and logical symbols in the definitions in the last chapter, by now you should be seeing them lurking beneath the surface. That fact that two functions
这一原理被称为函数外延性,类似于第 12.1 节讨论的集合外延性原理。回想一下符号
This principle is a known as function extensionality, analogous to the principle of extensionality for sets, discussed in Section 12.1. Recall that the notation
如果我们假设我们正在以逻辑形式主义的方式工作,并且基本类型为
We can avoid set-theoretic notation if we assume we are working in a logical formalism with basic types for
表示量化结束
to indicate that the quantification is over
函数
The function
和
and
如果
If
请注意,这是一个通用语句,它相当于
Notice that this is a universal statement, and it is equivalent to the statement that
请记住,在逻辑中,通常使用 lambda 符号来定义函数。我们可以将恒等函数表示为
Remember that in logic it is common to use lambda notation to define functions. We can denote the identity function by
还要记住,如果
Also remember that if
等价地,我们可以写
Equivalently, we can write
假设
Assuming
两者都可以被认为断言“
and both can be taken to assert that “the
二元关系
A binary relation
在这种情况下,逻辑学家可能会使用iota 符号,
In that case, a logician might use iota notation,
定义
to define
逻辑学家可能会使用希尔伯特 epsilon来定义函数
a logician might use the Hilbert epsilon to define a function
“选择”一个值
to “choose” a value of
16.2.二阶及高阶逻辑¶
16.2. Second- and Higher-Order Logic¶
与一阶逻辑相比,我们从固定的函数和关系符号开始,而前几章中讨论的主题鼓励我们考虑一种更具表达力的语言,其变量也涵盖函数和关系。例如,假设一个函数
In contrast to first-order logic, where we start with a fixed stock of function and relation symbols, the topics we have been considering in the last few chapters encourage us to consider a more expressive language with variables ranging over functions and relations as well. For example, saying that a function
该定理断言,如果任何函数
The theorem that asserts that if any function
类似地,说两个集合
Similarly, saying that two sets
这些陈述之所以有趣,是因为它们涉及函数和关系上的存在量化和通用量化。这超出了第一阶逻辑的范围。一种选择是用第一阶逻辑的语言开发一种理论,其中宇宙包含函数和关系作为对象;我们稍后会看到这就是公理集合论所做的。另一种方法是扩展第一阶逻辑以涉及新类型的量词和变量,以涵盖函数和关系。这就是高阶逻辑所做的。
What makes these statements interesting is that they involve quantification, both existential and universal, over functions and relations. This takes us outside the realm of first-order logic. One option is to develop a theory in the language of first-order logic in which the universe contains functions and relations as objects; we will see later that this is what axiomatic set theory does. An alternative is to extend first-order logic to involve new kinds of quantifiers and variables, to range over functions and relations. This is what higher-order logic does.
有多种方法可以实现这一点。鉴于前面描述的函数和关系之间的关系,人们可以将关系作为基础,并根据关系定义函数,反之亦然。以下高阶逻辑的表述,由逻辑学家阿隆佐·丘奇提出,遵循后一种方法。它有时被称为简单类型理论。
There are various ways to go about this. In view of the relationship between functions and relations described earlier, one can take relations as basic, and define functions in terms of them, or vice-versa. The following formulation of higher-order logic, due to the logician Alonzo Church, follows the latter approach. It is sometimes known as simple type theory.
从一些基本类型开始,
Start with some basic types,
如果
和 是类型,所以是 。If
and are types, so is .如果
和 是类型,所以是 。If
and are types, so is .
第一个旨在表示有序对的类型
The first intended to denote the type of ordered pairs
如果
属于类型 和 属于类型 , 属于类型 。If
is of type and is of type , is of type .如果
属于类型 , 然后 属于类型 和 如果类型 . (这些旨在表示对中的第一个和第二个元素 。If
is of type , then is of type and if of type . (These are intended to denote the first and second element of the pair .)如果
是类型的变量 , 和 是任何类型的表达式 , 然后 属于类型 。If
is a variable of type , and is any expression of type , then is of type .如果
属于类型 和 属于类型 , 属于类型 。If
is of type and is of type , is of type .
此外,简单类型理论提供了我们在一阶逻辑中拥有的所有方法来构建命题——布尔连接词、量词和相等性。
In addition, simple type theory provides all the means we have in first-order logic—boolean connectives, quantifiers, and equality—to build propositions.
函数
A function
我们没有非常仔细地指定高阶逻辑的语法和规则。这在许多更高级的逻辑教科书中都是这样做的。高阶逻辑的一部分只允许基本类型的函数和关系(不迭代这些构造),称为二阶逻辑。
We have not specified the syntax and rules of higher-order logic very carefully. This is done in a number of more advanced logic textbooks. The fragment of higher-order logic which allows only functions and relations on the basic types (without iterating these constructions) is known as second-order logic.
这些概念应该看起来很熟悉;我们在精益中一直使用这些结构,并带有类似的符号。事实上,精益的逻辑是一种更加复杂和富有表现力的逻辑系统,它完全涵盖了我们在此讨论的所有高阶逻辑概念。
These notions should seem familiar; we have been using these constructions, with similar notation, in Lean. Indeed, Lean’s logic is an even more elaborate and expressive system of logic, which fully subsumes all the notions of higher-order logic we have discussed here.
16.3. Lean 中的函数¶
16.3. Functions in Lean¶
我们讨论的概念具有如此简单的逻辑形式,这意味着在精益中定义它们很容易。精益中的正式表示和上述非正式表示之间的主要区别在于,在精益中,我们区分了类型X和该类型的子集。A : set X
The fact that the notions we have been discussing have such a straightforward logical form means that it is easy to define them in Lean. The main difference between the formal representation in Lean and the informal representation above is that, in Lean, we distinguish between a type X and a subset A : set X of that type.
在Lean的库中,组合和身份定义如下:
In Lean’s library, composition and identity are defined as follows:
variables {X Y Z : Type}
def comp (f : Y → Z) (g : X → Y) : X → Z :=
λx, f (g x)
infixr ` ∘ ` := comp
def id (x : X) : X :=
x
variables {X Y Z : Type}
def comp (f : Y → Z) (g : X → Y) : X → Z :=
λx, f (g x)
infixr ` ∘ ` := comp
def id (x : X) : X :=
x
通常我们用funext(表示“函数外延性”)来证明两个函数相等。
Ordinarily, we use funext (for “function extensionality”) to prove that two functions are equal.
example (f g : X → Y) (h : ∀ x, f x = g x) : f = g :=
funext h
example (f g : X → Y) (h : ∀ x, f x = g x) : f = g :=
funext h
但是,精益可以通过简单地展开定义和简化表达式,利用反身性来证明一些基本恒等式。
But Lean can prove some basic identities by simply unfolding definitions and simplifying expressions, using reflexivity.
lemma left_id (f : X → Y) : id ∘ f = f := rfl
lemma right_id (f : X → Y) : f ∘ id = f := rfl
theorem comp.assoc (f : Z → W) (g : Y → Z) (h : X → Y) :
(f ∘ g) ∘ h = f ∘ (g ∘ h) := rfl
theorem comp.left_id (f : X → Y) : id ∘ f = f := rfl
theorem comp.right_id (f : X → Y) : f ∘ id = f := rfl
lemma left_id (f : X → Y) : id ∘ f = f := rfl
lemma right_id (f : X → Y) : f ∘ id = f := rfl
theorem comp.assoc (f : Z → W) (g : Y → Z) (h : X → Y) :
(f ∘ g) ∘ h = f ∘ (g ∘ h) := rfl
theorem comp.left_id (f : X → Y) : id ∘ f = f := rfl
theorem comp.right_id (f : X → Y) : f ∘ id = f := rfl
我们可以定义它的含义
We can define what it means for
def injective (f : X → Y) : Prop :=
∀ ⦃x₁ x₂⦄, f x₁ = f x₂ → x₁ = x₂
def surjective (f : X → Y) : Prop :=
∀ y, ∃ x, f x = y
def bijective (f : X → Y) := injective f ∧ surjective f
def injective (f : X → Y) : Prop :=
∀ ⦃x₁ x₂⦄, f x₁ = f x₂ → x₁ = x₂
def surjective (f : X → Y) : Prop :=
∀ y, ∃ x, f x = y
def bijective (f : X → Y) := injective f ∧ surjective f
在 的定义中隐含地标记变量x₁和意味着我们不必经常写它们。具体来说,给定,和,我们写而不是来显示。x₂injectiveh : injective fh₁ : f x₁ = f x₂h h₁h x₁ x₂ h₁x₁ = x₂
Marking the variables x₁ and x₂ implicit in the definition of injective means that we do not have to write them as often. Specifically, given h : injective f, and h₁ : f x₁ = f x₂, we write h h₁ rather than h x₁ x₂ h₁ to show x₁ = x₂.
然后我们可以证明恒等函数是双射的:
We can then prove that the identity function is bijective:
theorem injective_id : injective (@id X) :=
assume x₁ x₂,
assume H : id x₁ = id x₂,
show x₁ = x₂, from H
theorem surjective_id : surjective (@id X) :=
assume y,
show ∃ x, id x = y, from exists.intro y rfl
theorem bijective_id : bijective (@id X) :=
and.intro injective_id surjective_id
theorem injective_id : injective (@id X) :=
assume x₁ x₂,
assume H : id x₁ = id x₂,
show x₁ = x₂, from H
theorem surjective_id : surjective (@id X) :=
assume y,
show ∃ x, id x = y, from exists.intro y rfl
theorem bijective_id : bijective (@id X) :=
and.intro injective_id surjective_id
更有趣的是,我们可以证明单射函数的复合是单射,等等。
More interestingly, we can prove that the composition of injective functions is injective, and so on.
theorem injective_comp {g : Y → Z} {f : X → Y}
(Hg : injective g) (Hf : injective f) :
injective (g ∘ f) :=
assume x₁ x₂,
assume : (g ∘ f) x₁ = (g ∘ f) x₂,
have f x₁ = f x₂, from Hg this,
show x₁ = x₂, from Hf this
theorem surjective_comp {g : Y → Z} {f : X → Y}
(hg : surjective g) (hf : surjective f) :
surjective (g ∘ f) :=
assume z,
exists.elim (hg z) $
assume y (hy : g y = z),
exists.elim (hf y) $
assume x (hx : f x = y),
have g (f x) = z, from eq.subst (eq.symm hx) hy,
show ∃ x, g (f x) = z, from exists.intro x this
theorem bijective_comp {g : Y → Z} {f : X → Y}
(hg : bijective g) (hf : bijective f) :
bijective (g ∘ f) :=
have ginj : injective g, from hg.left,
have gsurj : surjective g, from hg.right,
have finj : injective f, from hf.left,
have fsurj : surjective f, from hf.right,
and.intro (injective_comp ginj finj)
(surjective_comp gsurj fsurj)
theorem injective_comp {g : Y → Z} {f : X → Y}
(Hg : injective g) (Hf : injective f) :
injective (g ∘ f) :=
assume x₁ x₂,
assume : (g ∘ f) x₁ = (g ∘ f) x₂,
have f x₁ = f x₂, from Hg this,
show x₁ = x₂, from Hf this
theorem surjective_comp {g : Y → Z} {f : X → Y}
(hg : surjective g) (hf : surjective f) :
surjective (g ∘ f) :=
assume z,
exists.elim (hg z) $
assume y (hy : g y = z),
exists.elim (hf y) $
assume x (hx : f x = y),
have g (f x) = z, from eq.subst (eq.symm hx) hy,
show ∃ x, g (f x) = z, from exists.intro x this
theorem bijective_comp {g : Y → Z} {f : X → Y}
(hg : bijective g) (hf : bijective f) :
bijective (g ∘ f) :=
have ginj : injective g, from hg.left,
have gsurj : surjective g, from hg.right,
have finj : injective f, from hf.left,
have fsurj : surjective f, from hf.right,
and.intro (injective_comp ginj finj)
(surjective_comp gsurj fsurj)
左逆和右逆的概念按照预期的方式定义。
The notions of left and right inverse are defined in the expected way.
-- g is a left inverse to f
def left_inverse (g : Y → X) (f : X → Y) : Prop :=
∀ x, g (f x) = x
-- g is a right inverse to f
def right_inverse (g : Y → X) (f : X → Y) : Prop :=
left_inverse f g
-- g is a left inverse to f
def left_inverse (g : Y → X) (f : X → Y) : Prop :=
∀ x, g (f x) = x
-- g is a right inverse to f
def right_inverse (g : Y → X) (f : X → Y) : Prop :=
left_inverse f g
具体来说,与左逆或右逆组合可得到恒等式。
In particular, composing with a left or right inverse yields the identity.
def id_of_left_inverse {g : Y → X} {f : X → Y} :
left_inverse g f → g ∘ f = id :=
assume H, funext H
def id_of_right_inverse {g : Y → X} {f : X → Y} :
right_inverse g f → f ∘ g = id :=
assume H, funext H
def id_of_left_inverse {g : Y → X} {f : X → Y} :
left_inverse g f → g ∘ f = id :=
assume H, funext H
def id_of_right_inverse {g : Y → X} {f : X → Y} :
right_inverse g f → f ∘ g = id :=
assume H, funext H
注意,我们需要用funext来证明函数的相等性。
Notice that we need to use funext to show the equality of functions.
下面证明,如果一个函数有左逆,那么它就是单射,如果它有右逆,那么它就是全射。
The following shows that if a function has a left inverse, then it is injective, and if it has a right inverse, then it is surjective.
theorem injective_of_left_inverse {g : Y → X} {f : X → Y} :
left_inverse g f → injective f :=
assume h, assume x₁ x₂, assume feq,
calc x₁ = g (f x₁) : by rw h
... = g (f x₂) : by rw feq
... = x₂ : by rw h
theorem surjective_of_right_inverse {g : Y → X} {f : X → Y} :
right_inverse g f → surjective f :=
assume h, assume y,
let x : X := g y in
have f x = y, from calc
f x = (f (g y)) : rfl
... = y : by rw [h y],
show ∃ x, f x = y, from exists.intro x this
theorem injective_of_left_inverse {g : Y → X} {f : X → Y} :
left_inverse g f → injective f :=
assume h, assume x₁ x₂, assume feq,
calc x₁ = g (f x₁) : by rw h
... = g (f x₂) : by rw feq
... = x₂ : by rw h
theorem surjective_of_right_inverse {g : Y → X} {f : X → Y} :
right_inverse g f → surjective f :=
assume h, assume y,
let x : X := g y in
have f x = y, from calc
f x = (f (g y)) : rfl
... = y : by rw [h y],
show ∃ x, f x = y, from exists.intro x this
16.4.经典地定义逆¶
16.4. Defining the Inverse Classically¶
上一节列出的所有定理都可以在 Lean 库中找到,当您使用以下命令打开函数命名空间时,它们都可供您使用:open function
All the theorems listed in the previous section are found in the Lean
library, and are available to you when you open the function namespace
with open function:
open function
#check comp
#check left_inverse
#check has_right_inverse
open function
#check comp
#check left_inverse
#check has_right_inverse
然而,定义反函数需要经典推理,我们可以通过打开经典命名空间来获得:
Defining inverse functions, however, requires classical reasoning, which we get by opening the classical namespace:
open classical
section
variables A B : Type
variable P : A → Prop
variable R : A → B → Prop
example : (∀ x, ∃ y, R x y) → ∃ f : A → B, ∀ x, R x (f x) :=
axiom_of_choice
example (h : ∃ x, P x) : P (some h) :=
some_spec h
end
open classical
section
variables A B : Type
variable P : A → Prop
variable R : A → B → Prop
example : (∀ x, ∃ y, R x y) → ∃ f : A → B, ∀ x, R x (f x) :=
axiom_of_choice
example (h : ∃ x, P x) : P (some h) :=
some_spec h
end
选择公理告诉我们,如果对于每个,都有满足 的,那么就存在一个函数,它对于每个 都选择这样的。在精益中,这个“公理”是使用经典构造来证明的,即函数(有时称为“不定描述运算符”),给定某个满足 的,返回这样的。有了这些构造,反函数定义如下:x : Xy : YR x yf : X → YxysomexP xx
The axiom of choice tells us that if, for every x : X, there is a y : Y satisfying R x y, then there is a function f : X → Y which, for every x chooses such a y. In Lean, this “axiom” is proved using a classical construction, the some function (sometimes called “the indefinite description operator”) which, given that there is some x satisfying P x, returns such an x. With these constructions, the inverse function is defined as follows:
open classical function
local attribute [instance] prop_decidable
variables {X Y : Type}
noncomputable def inverse (f : X → Y) (default : X) : Y → X :=
λ y, if h : ∃ x, f x = y then some h else default
open classical function
local attribute [instance] prop_decidable
variables {X Y : Type}
noncomputable def inverse (f : X → Y) (default : X) : Y → X :=
λ y, if h : ∃ x, f x = y then some h else default
精益要求我们承认该定义不是计算性的,因为首先,从算法上来说可能无法决定条件是否h成立,即使成立,从算法上来说也可能无法找到合适的值x。
Lean requires us to acknowledge that the definition is not computational, since, first, it may not be algorithmically possible to decide whether or not condition h holds, and even if it does, it may not be algorithmically possible to find a suitable value of x.
下面,命题inverse_of_exists断言inverse满足其规范,后续定理表明,如果f是单射,则inverse函数确实是左逆。
Below, the proposition inverse_of_exists asserts that inverse meets its specification, and the subsequent theorem shows that if f is injective, then the inverse function really is a left inverse.
theorem inverse_of_exists (f : X → Y) (default : X) (y : Y)
(h : ∃ x, f x = y) :
f (inverse f default y) = y :=
have h1 : inverse f default y = some h, from dif_pos h,
have h2 : f (some h) = y, from some_spec h,
eq.subst (eq.symm h1) h2
theorem is_left_inverse_of_injective (f : X → Y) (default : X)
(injf : injective f) :
left_inverse (inverse f default) f :=
let finv := (inverse f default) in
assume x,
have h1 : ∃ x', f x' = f x, from exists.intro x rfl,
have h2 : f (finv (f x)) = f x, from inverse_of_exists f default (f x) h1,
show finv (f x) = x, from injf h2
theorem inverse_of_exists (f : X → Y) (default : X) (y : Y)
(h : ∃ x, f x = y) :
f (inverse f default y) = y :=
have h1 : inverse f default y = some h, from dif_pos h,
have h2 : f (some h) = y, from some_spec h,
eq.subst (eq.symm h1) h2
theorem is_left_inverse_of_injective (f : X → Y) (default : X)
(injf : injective f) :
left_inverse (inverse f default) f :=
let finv := (inverse f default) in
assume x,
have h1 : ∃ x', f x' = f x, from exists.intro x rfl,
have h2 : f (finv (f x)) = f x, from inverse_of_exists f default (f x) h1,
show finv (f x) = x, from injf h2
16.5. Lean 中的函数和集合¶
16.5. Functions and Sets in Lean¶
在7.4 节中,我们了解了在形式化短语(如“每个大于 2 的素数都是奇数”和“某个素数是偶数”)时如何表示相对化的全称量词和存在量词。类似地,我们可以将语句相对化为集合。在符号逻辑中,表达式
In Section 7.4 we saw how to represent relativized universal and existential quantifiers when formalizing phrases like “every prime number greater than two is odd” and “some prime number is even.” In a similar way, we can relativize statements to sets. In symbolic logic, the expression
Lean 还定义了相对量词的符号:
Lean also defines notation for relativized quantifiers:
variables (X : Type) (A : set X) (P : X → Prop)
#check ∀ x ∈ A, P x
#check ∃ x ∈ A, P x
variables (X : Type) (A : set X) (P : X → Prop)
#check ∀ x ∈ A, P x
#check ∃ x ∈ A, P x
下面是如何使用有界通用量词的一个例子:
Here is an example of how to use the bounded universal quantifier:
example (h : ∀ x ∈ A, P x) (x : X) (h1 : x ∈ A) : P x := h x h1
example (h : ∀ x ∈ A, P x) (x : X) (h1 : x ∈ A) : P x := h x h1
使用有界量词,我们可以讨论函数在特定集合上的行为:
Using bounded quantifiers, we can talk about the behavior of functions on particular sets:
import data.set
open set function
variables {X Y : Type}
variables (A : set X) (B : set Y)
def maps_to (f : X → Y) (A : set X) (B : set Y) :=
∀ {x}, x ∈ A → f x ∈ B
def inj_on (f : X → Y) (A : set X) :=
∀ {x₁ x₂}, x₁ ∈ A → x₂ ∈ A → f x₁ = f x₂ → x₁ = x₂
def surj_on (f : X → Y) (A : set X) (B : set Y) := B ⊆ f '' A
import data.set
open set function
variables {X Y : Type}
variables (A : set X) (B : set Y)
def maps_to (f : X → Y) (A : set X) (B : set Y) :=
∀ {x}, x ∈ A → f x ∈ B
def inj_on (f : X → Y) (A : set X) :=
∀ {x₁ x₂}, x₁ ∈ A → x₂ ∈ A → f x₁ = f x₂ → x₁ = x₂
def surj_on (f : X → Y) (A : set X) (B : set Y) := B ⊆ f '' A
表达式断言将集合的元素映射到集合,表达式断言是 上的单射。表达式断言,如果将 视为定义在 元素上的函数,则 函数是 上的单射。以下是如何使用它们的示例:maps_to f A BfABinj_on f AfAsurj_on f A BAfB
The expression maps_to f A B asserts that f maps elements of the set A to the set B, and the expression inj_on f A asserts that f is injective on A. The expression surj_on f A B asserts that, viewed as a function defined on elements of A, the function f is surjective onto the set B. Here are examples of how they can be used:
variables (f : X → Y) (A : set X) (B : set Y)
example (h : maps_to f A B) (x : X) (h1 : x ∈ A) : f x ∈ B := h h1
example (h : inj_on f A) (x₁ x₂ : X) (h1 : x₁ ∈ A) (h2 : x₂ ∈ A)
(h3 : f x₁ = f x₂) : x₁ = x₂ :=
h h1 h2 h3
variables (f : X → Y) (A : set X) (B : set Y)
example (h : maps_to f A B) (x : X) (h1 : x ∈ A) : f x ∈ B := h h1
example (h : inj_on f A) (x₁ x₂ : X) (h1 : x₁ ∈ A) (h2 : x₂ ∈ A)
(h3 : f x₁ = f x₂) : x₁ = x₂ :=
h h1 h2 h3
在下面的例子中,我们将使用带有隐式参数的版本。表达式断言,如果将其视为定义在 元素上的函数,则该函数对集合 是全射。surj_on f A BAfB
In the examples below, we’ll use the versions with implicit arguments. The expression surj_on f A B asserts that, viewed as a function defined on elements of A, the function f is surjective onto the set B.
有了这些概念,我们就可以证明单射函数的复合是单射。证明与上面的类似,但现在我们必须更加小心地将对A和的断言相对化B:
With these notions in hand, we can prove that the composition of injective functions is injective. The proof is similar to the one above, though now we have to be more careful to relativize claims to A and B:
theorem inj_on_comp (fAB : maps_to f A B) (hg : inj_on g B) (hf: inj_on f A) :
inj_on (g ∘ f) A :=
assume x1 : X,
assume x1A : x1 ∈ A,
assume x2 : X,
assume x2A : x2 ∈ A,
have fx1B : f x1 ∈ B, from fAB x1A,
have fx2B : f x2 ∈ B, from fAB x2A,
assume h1 : g (f x1) = g (f x2),
have h2 : f x1 = f x2, from hg fx1B fx2B h1,
show x1 = x2, from hf x1A x2A h2
theorem inj_on_comp (fAB : maps_to f A B) (hg : inj_on g B) (hf: inj_on f A) :
inj_on (g ∘ f) A :=
assume x1 : X,
assume x1A : x1 ∈ A,
assume x2 : X,
assume x2A : x2 ∈ A,
have fx1B : f x1 ∈ B, from fAB x1A,
have fx2B : f x2 ∈ B, from fAB x2A,
assume h1 : g (f x1) = g (f x2),
have h2 : f x1 = f x2, from hg fx1B fx2B h1,
show x1 = x2, from hf x1A x2A h2
类似地,我们可以证明全射函数的复合是全射:
We can similarly prove that the composition of surjective functions is surjective:
theorem surj_on_comp (hg : surj_on g B C) (hf: surj_on f A B) :
surj_on (g ∘ f) A C :=
assume z,
assume zc : z ∈ C,
exists.elim (hg zc) $
assume y (h1 : y ∈ B ∧ g y = z),
exists.elim (hf (and.left h1)) $
assume x (h2 : x ∈ A ∧ f x = y),
show ∃x, x ∈ A ∧ g (f x) = z, from
exists.intro x
(and.intro
(and.left h2)
(calc
g (f x) = g y : by rw and.right h2
... = z : by rw and.right h1))
theorem surj_on_comp (hg : surj_on g B C) (hf: surj_on f A B) :
surj_on (g ∘ f) A C :=
assume z,
assume zc : z ∈ C,
exists.elim (hg zc) $
assume y (h1 : y ∈ B ∧ g y = z),
exists.elim (hf (and.left h1)) $
assume x (h2 : x ∈ A ∧ f x = y),
show ∃x, x ∈ A ∧ g (f x) = z, from
exists.intro x
(and.intro
(and.left h2)
(calc
g (f x) = g y : by rw and.right h2
... = z : by rw and.right h1))
下面显示并集的图像是图像的并集:
The following shows that the image of a union is the union of images:
theorem image_union : f '' (A₁ ∪ A₂) =f '' A₁ ∪ f '' A₂ :=
ext (assume y, iff.intro
(assume h : y ∈ image f (A₁ ∪ A₂),
exists.elim h $
assume x h1,
have xA₁A₂ : x ∈ A₁ ∪ A₂, from h1.left,
have fxy : f x = y, from h1.right,
or.elim xA₁A₂
(assume xA₁, or.inl ⟨x, xA₁, fxy⟩)
(assume xA₂, or.inr ⟨x, xA₂, fxy⟩))
(assume h : y ∈ image f A₁ ∪ image f A₂,
or.elim h
(assume yifA₁ : y ∈ image f A₁,
exists.elim yifA₁ $
assume x h1,
have xA₁ : x ∈ A₁, from h1.left,
have fxy : f x = y, from h1.right,
⟨x, or.inl xA₁, fxy⟩)
(assume yifA₂ : y ∈ image f A₂,
exists.elim yifA₂ $
assume x h1,
have xA₂ : x ∈ A₂, from h1.left,
have fxy : f x = y, from h1.right,
⟨x, (or.inr xA₂), fxy⟩)))
theorem image_union : f '' (A₁ ∪ A₂) =f '' A₁ ∪ f '' A₂ :=
ext (assume y, iff.intro
(assume h : y ∈ image f (A₁ ∪ A₂),
exists.elim h $
assume x h1,
have xA₁A₂ : x ∈ A₁ ∪ A₂, from h1.left,
have fxy : f x = y, from h1.right,
or.elim xA₁A₂
(assume xA₁, or.inl ⟨x, xA₁, fxy⟩)
(assume xA₂, or.inr ⟨x, xA₂, fxy⟩))
(assume h : y ∈ image f A₁ ∪ image f A₂,
or.elim h
(assume yifA₁ : y ∈ image f A₁,
exists.elim yifA₁ $
assume x h1,
have xA₁ : x ∈ A₁, from h1.left,
have fxy : f x = y, from h1.right,
⟨x, or.inl xA₁, fxy⟩)
(assume yifA₂ : y ∈ image f A₂,
exists.elim yifA₂ $
assume x h1,
have xA₂ : x ∈ A₂, from h1.left,
have fxy : f x = y, from h1.right,
⟨x, (or.inr xA₂), fxy⟩)))
请注意,表达式扩展为。因此,我们需要提供三条信息: 的值、 的证明和 的证明。在此证明的第八行, 之后,我们可以通过在括号中写入来证明必要的信息。但在这种情况下,Lean 的“匿名构造函数”符号,即用和输入的角括号,允许我们使用更紧凑的表达式。y ∈ image f A₁∃ x, x ∈ A₁ ∧ f x = yxx ∈ A₁f x = yor.inlexists.intro x (and.intro xA₁ fxy)\<\>⟨x, xA₁, fxy⟩
Note that the expression y ∈ image f A₁ expands to ∃ x, x ∈ A₁ ∧ f x = y. We therefore need to provide three pieces of information: a value of x, a proof that x ∈ A₁, and a proof that f x = y. On the eighth line of this proof, after or.inl, we could justify the necessary information by writing exists.intro x (and.intro xA₁ fxy) in parentheses. But in this case Lean’s “anonymous constructor” notation, that is, the corner brackets entered with \< and \>, allow us to use the more compact expression ⟨x, xA₁, fxy⟩.
16.6.练习¶
16.6. Exercises¶
填写
sorry下面最后三个证明中的 。import data.set data.int.basic open function int def f (x : ℤ) : ℤ := x + 3 def g (x : ℤ) : ℤ := -x def h (x : ℤ) : ℤ := 2 * x + 3 example : injective f := assume x1 x2, assume h1 : x1 + 3 = x2 + 3, -- Lean knows this is the same as f x1 = f x2 show x1 = x2, from add_right_cancel h1 example : surjective f := assume y, have h1 : f (y - 3) = y, from calc f (y - 3) = (y - 3) + 3 : rfl ... = y : by rw sub_add_cancel, show ∃ x, f x = y, from exists.intro (y - 3) h1 example (x y : ℤ) (h : 2 * x = 2 * y) : x = y := have h1 : 2 ≠ (0 : ℤ), from dec_trivial, -- this tells Lean to figure it out itself show x = y, from mul_left_cancel' h1 h example (x : ℤ) : -(-x) = x := neg_neg x example (A B : Type) (u : A → B) (v : B → A) (h : left_inverse u v) : ∀ x, u (v x) = x := h example (A B : Type) (u : A → B) (v : B → A) (h : left_inverse u v) : right_inverse v u := h -- fill in the sorry's in the following proofs example : injective h := sorry example : surjective g := sorry example (A B : Type) (u : A → B) (v1 : B → A) (v2 : B → A) (h1 : left_inverse v1 u) (h2 : right_inverse v2 u) : v1 = v2 := funext (assume x, calc v1 x = v1 (u (v2 x)) : sorry ... = v2 x : sorry)
Fill in the
sorry’s in the last three proofs below.import data.set data.int.basic open function int def f (x : ℤ) : ℤ := x + 3 def g (x : ℤ) : ℤ := -x def h (x : ℤ) : ℤ := 2 * x + 3 example : injective f := assume x1 x2, assume h1 : x1 + 3 = x2 + 3, -- Lean knows this is the same as f x1 = f x2 show x1 = x2, from add_right_cancel h1 example : surjective f := assume y, have h1 : f (y - 3) = y, from calc f (y - 3) = (y - 3) + 3 : rfl ... = y : by rw sub_add_cancel, show ∃ x, f x = y, from exists.intro (y - 3) h1 example (x y : ℤ) (h : 2 * x = 2 * y) : x = y := have h1 : 2 ≠ (0 : ℤ), from dec_trivial, -- this tells Lean to figure it out itself show x = y, from mul_left_cancel' h1 h example (x : ℤ) : -(-x) = x := neg_neg x example (A B : Type) (u : A → B) (v : B → A) (h : left_inverse u v) : ∀ x, u (v x) = x := h example (A B : Type) (u : A → B) (v : B → A) (h : left_inverse u v) : right_inverse v u := h -- fill in the sorry's in the following proofs example : injective h := sorry example : surjective g := sorry example (A B : Type) (u : A → B) (v1 : B → A) (v2 : B → A) (h1 : left_inverse v1 u) (h2 : right_inverse v2 u) : v1 = v2 := funext (assume x, calc v1 x = v1 (u (v2 x)) : sorry ... = v2 x : sorry)
填写
sorry下面的证明。import data.set open function set variables {X Y : Type} variable f : X → Y variables A B : set X example : f '' (A ∪ B) = f '' A ∪ f '' B := eq_of_subset_of_subset (assume y, assume h1 : y ∈ f '' (A ∪ B), exists.elim h1 $ assume x h, have h2 : x ∈ A ∪ B, from h.left, have h3 : f x = y, from h.right, or.elim h2 (assume h4 : x ∈ A, have h5 : y ∈ f '' A, from ⟨x, h4, h3⟩, show y ∈ f '' A ∪ f '' B, from or.inl h5) (assume h4 : x ∈ B, have h5 : y ∈ f '' B, from ⟨x, h4, h3⟩, show y ∈ f '' A ∪ f '' B, from or.inr h5)) (assume y, assume h2 : y ∈ f '' A ∪ f '' B, or.elim h2 (assume h3 : y ∈ f '' A, exists.elim h3 $ assume x h, have h4 : x ∈ A, from h.left, have h5 : f x = y, from h.right, have h6 : x ∈ A ∪ B, from or.inl h4, show y ∈ f '' (A ∪ B), from ⟨x, h6, h5⟩) (assume h3 : y ∈ f '' B, exists.elim h3 $ assume x h, have h4 : x ∈ B, from h.left, have h5 : f x = y, from h.right, have h6 : x ∈ A ∪ B, from or.inr h4, show y ∈ f '' (A ∪ B), from ⟨x, h6, h5⟩)) -- remember, x ∈ A ∩ B is the same as x ∈ A ∧ x ∈ B example (x : X) (h1 : x ∈ A) (h2 : x ∈ B) : x ∈ A ∩ B := and.intro h1 h2 example (x : X) (h1 : x ∈ A ∩ B) : x ∈ A := and.left h1 -- Fill in the proof below. -- (It should take about 8 lines.) example : f '' (A ∩ B) ⊆ f '' A ∩ f '' B := assume y, assume h1 : y ∈ f '' (A ∩ B), show y ∈ f '' A ∩ f '' B, from sorry
Fill in the
sorryin the proof below.import data.set open function set variables {X Y : Type} variable f : X → Y variables A B : set X example : f '' (A ∪ B) = f '' A ∪ f '' B := eq_of_subset_of_subset (assume y, assume h1 : y ∈ f '' (A ∪ B), exists.elim h1 $ assume x h, have h2 : x ∈ A ∪ B, from h.left, have h3 : f x = y, from h.right, or.elim h2 (assume h4 : x ∈ A, have h5 : y ∈ f '' A, from ⟨x, h4, h3⟩, show y ∈ f '' A ∪ f '' B, from or.inl h5) (assume h4 : x ∈ B, have h5 : y ∈ f '' B, from ⟨x, h4, h3⟩, show y ∈ f '' A ∪ f '' B, from or.inr h5)) (assume y, assume h2 : y ∈ f '' A ∪ f '' B, or.elim h2 (assume h3 : y ∈ f '' A, exists.elim h3 $ assume x h, have h4 : x ∈ A, from h.left, have h5 : f x = y, from h.right, have h6 : x ∈ A ∪ B, from or.inl h4, show y ∈ f '' (A ∪ B), from ⟨x, h6, h5⟩) (assume h3 : y ∈ f '' B, exists.elim h3 $ assume x h, have h4 : x ∈ B, from h.left, have h5 : f x = y, from h.right, have h6 : x ∈ A ∪ B, from or.inr h4, show y ∈ f '' (A ∪ B), from ⟨x, h6, h5⟩)) -- remember, x ∈ A ∩ B is the same as x ∈ A ∧ x ∈ B example (x : X) (h1 : x ∈ A) (h2 : x ∈ B) : x ∈ A ∩ B := and.intro h1 h2 example (x : X) (h1 : x ∈ A ∩ B) : x ∈ A := and.left h1 -- Fill in the proof below. -- (It should take about 8 lines.) example : f '' (A ∩ B) ⊆ f '' A ∩ f '' B := assume y, assume h1 : y ∈ f '' (A ∩ B), show y ∈ f '' A ∩ f '' B, from sorry
17.自然数与归纳法¶
17. The Natural Numbers and Induction¶
本章标志着从抽象到具体的过渡。从集合、关系和函数的角度看待数学世界,为我们提供了思考数学对象和结构及其之间关系的有用方法。然而,在某个时候,我们需要开始思考特定的数学对象和结构,而自然数是一个很好的起点。19 世纪数学家利奥波德·克罗内克曾宣称“上帝创造了整数;其他一切都是人类的杰作。”他这样说的意思是,自然数(以及整数,我们也将在下面讨论)是数学世界的基本组成部分,许多其他感兴趣的对象和结构都可以从这些对象和结构中构建出来。
This chapter marks a transition from the abstract to the concrete. Viewing the mathematical universe in terms of sets, relations, and functions gives us useful ways of thinking about mathematical objects and structures and the relationships between them. At some point, however, we need to start thinking about particular mathematical objects and structures, and the natural numbers are a good place to start. The nineteenth century mathematician Leopold Kronecker once proclaimed “God created the whole numbers; everything else is the work of man.” By this he meant that the natural numbers (and the integers, which we will also discuss below) are a fundamental component of the mathematical universe, and that many other objects and structures of interest can be constructed from these.
在本章中,我们将考虑自然数及其基本原理。在第 18 章中,我们将看到,即使是加法和乘法等基本运算也可以使用此处描述的方法进行定义,并且它们的属性可以从这些基本原理中推导出来。不过,我们在本章中的介绍将保持非正式。在第 19 章中,我们将看到这些原理如何在数论中发挥作用,数论是数学中最古老、最受尊敬的分支之一。
In this chapter, we will consider the natural numbers and the basic principles that govern them. In Chapter 18 we will see that even basic operations like addition and multiplication can be defined using means described here, and their properties derived from these basic principles. Our presentation in this chapter will remain informal, however. In Chapter 19, we will see how these principles play out in number theory, one of the oldest and most venerable branches of mathematics.
17.1.归纳原理¶
17.1. The Principle of Induction¶
自然数集是
The set of natural numbers is the set
过去,人们对于自然数集应该以 0 还是 1 开头有不同的看法,但如今大多数数学家都认为自然数集以 0 开头。逻辑学家通常将函数称为
In the past, opinions have differed as to whether the set of natural numbers should start with 0 or 1, but these days most mathematicians take them to start with 0. Logicians often call the function
归纳原理。设
Principle of Induction. Let
这反映了自然数由零和后继运算生成的图像:通过覆盖零和后继的情况,我们可以处理所有自然数。
This reflects the image of the natural numbers as being generated by zero and the successor operation: by covering the zero and successor cases, we take care of all the natural numbers.
归纳原理提供了一种证明每个自然数都有某种属性的方法:证明
The principle of induction provides a recipe for proving that every natural number has a certain property: to show that
你可以将归纳证明想象成一种一次性推倒无数张多米诺骨牌的方法。我们设置好机制,推倒多米诺骨牌 0(基本情况),每张多米诺骨牌都会推倒下一张多米诺骨牌(归纳步骤)。因此,多米诺骨牌 0 会推倒多米诺骨牌 1;多米诺骨牌 1 会推倒多米诺骨牌 2,依此类推。
You can visualize proof by induction as a method of knocking down an infinite stream of dominoes, all at once. We set the mechanism in place and knock down domino 0 (the base case), and every domino knocks down the next domino (the induction step). So domino 0 knocks down domino 1; that knocks down domino 2, and so on.
这是一个归纳证明的例子。
Here is an example of a proof by induction.
定理。对于每个自然数
Theorem. For every natural number
证明。我们通过归纳法证明
Proof. We prove this by induction on
对于诱导步骤,修复
For the induction step, fix
我们需要证明这一说法同样适用于
We need to show that this same claim holds with
在一阶逻辑符号中,如果我们写
In the notation of first-order logic, if we write
但请注意,归纳原理表明,公理适用于每个属性
But notice that the principle of induction says that the axiom holds for every property
对属性进行量化使我们超出了一阶逻辑的范畴;因此,归纳推理是二阶原理。
Quantifying over properties takes us out of the realm of first-order logic; induction is therefore a second-order principle.
归纳证明的模式可以通过以下自然演绎规则更自然地表达:
The pattern for a proof by induction is expressed even more naturally by the following natural deduction rule:
你应该思考如何用自然演绎来正式表示本章中的某些证明。
You should think about how some of the proofs in this chapter could be represented formally using natural deduction.
另一个归纳证明的例子是,我们推导出一个公式,给定任意有限集
For another example of a proof by induction, let us derive a formula that, given any finite set
定理。对于任何有限集
Theorem. For any finite set
证明。我们利用归纳法
Proof. We use induction on
在归纳情况下,假设
In the inductive case, suppose
首先,我们考虑
First, we consider the subsets of
接下来我们考虑
Next we consider the subsets of
综合起来,有
Taken together, then, there are
我们已经看到,域的属性与域的子集之间存在对应关系。对于每个属性
We have seen that there is a correspondence between properties of a domain and subsets of a domain. For every property
归纳原理。设
Principle of Induction. Let
在这里,这么说
Here, saying that
17.2.归纳法的变体¶
17.2. Variants of Induction¶
在本节中,我们将讨论归纳原理中经常用到的变体。重要的是要认识到,这些变体都可以使用上一节中所述的归纳原理来证明,因此不必将它们视为基本原理。
In this section, we will consider variations on the principle of induction that are often useful. It is important to recognize that each of these can be justified using the principle of induction as stated in the last section, so they need not be taken as fundamental.
第一个没什么了不起的:而不是从
The first one is no great shakes: instead of starting from
从起点归纳原理。设
Principle of Induction from a Starting Point. Let
假设最后这条原则的假设,如果我们让
Assuming the hypotheses of this last principle, if we let
下面是使用此种归纳法变体的证明的一个例子。
Here is one example of a proof using this variant of induction.
定理。对于每个自然数
Theorem. For every natural number
证明。通过归纳
Proof. By induction on
对于诱导步骤,假设
For the induction step, suppose
再举一个例子,让我们推导出凸多边形内角和的公式。如果两个顶点之间的每条线都在多边形内,则称该多边形为凸多边形。我们将无需证明就接受这个显而易见的事实:任何边多于三条的凸多边形都可以细分为三角形和边少一条的凸多边形,即通过封闭任意两条连续边形成三角形。我们还会无需证明就接受这个基本的几何事实:任何三角形的内角和都是 180 度。
For another example, let us derive a formula for the sum total of the angles in a convex polygon. A polygon is said to be convex if every line between two vertices stays inside the polygon. We will accept without proof the visually obvious fact that one can subdivide any convex polygon with more than three sides into a triangle and a convex polygon with one fewer side, namely, by closing off any two consecutive sides to form a triangle. We will also accept, without proof, the basic geometric fact that the sum of the angles of any triangle is 180 degrees.
定理。对于任意
Theorem. For any
证明。在基本情况下,当
Proof. In the base case, when
对于诱导步骤,假设
For the induction step, suppose
对于我们的第二个例子,我们将考虑完全诱导原理,有时也称为完全诱导。
For our second example, we will consider the principle of complete induction, also sometimes known as total induction.
完全归纳原理。设
Principle of Complete Induction. Let
请注意,没有必要为零单独列出特殊情况:对于任何属性
Notice that there is no need to break out a special case for zero: for any property
还要注意,如果这样的属性
Notice also that if such a property
要使用完全归纳原理,我们只需要让
To use the principle of complete induction we merely have to let
定理。每个大于或等于 2 的自然数都可以写成素数的乘积。
Theorem. Every natural number greater than or equal to 2 can be written as a product of primes.
证明。我们通过归纳法进行
Proof. We proceed by induction on
但后来我们
But then we have
我们看到
We see that
最后,我们将考虑归纳法的另一种表述,即最小元素原理。
Finally, we will consider another formulation of induction, known as the least element principle.
最小元素原理。假设
The Least Element Principle. Suppose
事实上,用经典推理来说,这相当于完全归纳原理。要理解这一点,请考虑上述陈述的逆否命题:“如果不存在最小值,
In fact, using classical reasoning, this is equivalent to the principle of complete induction. To see this, consider the contrapositive of the statement above: “if there is no smallest value for which
因此,最小元素原理的使用方式与完全归纳原理的使用方式非常相似,这并不奇怪。例如,下面是上述证明的表述。请注意,它被表述为矛盾证明。
It is not surprising, then, that the least element principle can be used in much the same way as the principle of complete induction. Here, for example, is a formulation of the previous proof in these terms. Notice that it is phrased as a proof by contradiction.
定理。每个大于等于 2 的自然数都可以写成素数的乘积。
Theorem. Every natural number greater than equal to 2 can be written as a product of primes.
证明。相反,假设某个大于或等于 2 的自然数不能写成素数的乘积。根据最小元素原理,存在一个最小的这样的元素;称之为
Proof. Suppose, to the contrary, some natural number greater than or equal to 2 cannot be written as a product of primes. By the least element principle, there is a smallest such element; call it
但后来我们
But then we have
我们看到
We see that
下面是另一个示例:
Here is another example:
定理。每个自然数都是有趣的。
Theorem. Every natural number is interesting.
证明。相反,假设某个自然数是无趣的。那么有一个最小的自然数,
Proof. Suppose, to the contrary, some natural number is uninteresting. Then there is a smallest one,
17.3.递归定义¶
17.3. Recursive Definitions¶
假设我告诉你我有一个函数
Suppose I tell you that I have a function
你能推断出
What can you infer about
很快就会发现,对于每一个
It soon becomes apparent that for every
更有趣的是,上述两个条件指定了
What is more interesting is that the two conditions above specify all the values of
递归定义原理。设
Principle of Definition by Recursion. Let
递归定义原理同时提出了两个主张:首先,存在一个函数
The principle of recursive definition makes two claims at once: first, that there is a function
在一些公理框架中,递归定义原理可以用归纳原理来证明。在其他框架中,归纳原理可以看作递归定义原理的一个特例。现在,我们先简单地将两者都视为自然数的基本性质。
In some axiomatic frameworks, the principle of recursive definition can be justified using the principle of induction. In others, the principle of induction can be viewed as a special case of the principle of recursive definition. For now, we will simply take both to be fundamental properties of the natural numbers.
作为递归定义的另一个例子,考虑函数
As another example of a recursive definition, consider the function
尝试计算前几个值。解开定义,我们看到
Try calculating the first few values. Unwrapping the definition, we see that
事实上,求和符号
Indeed, summation notation
和产品符号
and product notation
也可以使用递归定义使其精确化。例如,函数
can also be made precise using recursive definitions. For example, the function
归纳和递归是互补的原理,通常证明递归定义函数的方法是使用归纳原理。例如,以下定理提供了求和公式
Induction and recursion are complementary principles, and typically the way to prove something about a recursively defined function is to use the principle of induction. For example, the following theorem provides a formulas for the sum
定理。对于每一个
Theorem. For every
证明。在基本情况下,当
Proof. In the base case, when
在归纳步骤中,我们有
In the inductive step, we have
递归定义原理的变体与归纳原理的变体一样多。例如,与完全归纳原理类似,我们可以指定
There are just as many variations on the principle of recursive definition as there are on the principle of induction. For example, in analogy to the principle of complete induction, we can specify a value of
计算值
Calculating the values of
这里,第二个数字之后的每个连续数字都是它前面两个值的总和。这被称为斐波那契数列,相应的数字称为斐波那契数。普通的数学表示会写成
Here, after the second number, each successive number is the sum of the two values preceding it. This is known as the Fibonacci sequence, and the corresponding numbers are known as the Fibonacci numbers. An ordinary mathematical presentation would write
但是现在您可以认识到这种规范是对递归定义原理的隐含诉求。我们要求您在下面的练习中证明有关斐波那契数列的一些事实。
But you can now recognize such a specification as an implicit appeal to the principle of definition by recursion. We ask you to prove some facts about the Fibonacci sequence in the exercises below.
17.4.定义算术运算¶
17.4. Defining Arithmetic Operations¶
事实上,我们甚至可以使用递归定义原理来定义自然数的最基本运算,并表明它们具有我们期望的属性。从基础的角度来看,我们可以将自然数描述为一个集合,
In fact, we can even use the principle of recursive definition to define the most basic operations on the natural numbers and show that they have the properties we expect them to have. From a foundational standpoint, we can characterize the natural numbers as a set,
对于任意 在 。 for any in .对于每一个
和 在 , 如果 , 然后 。 换句话说, 是单射。For every
and in , if , then . In other words, is injective.如果
是 具有以下属性 位于 并且无论何时 位于 然后 位于 , 然后 。If
is any subset of with the property that is in and whenever is in then is in , then .
最后一句可以改写为归纳原则:
The last clause can be reformulated as the principle of induction:
认为
是自然数的任何属性,例如 持有 ,并且对于每一个 , 暗示 .然后每个 对每个自然数都成立。
Suppose
is any property of natural numbers, such that holds of , and for every , implies . Then every holds of every natural number.
请记住,此原理可用于证明递归定义原理:
Remember that this principle can be used to justify the principle of definition by recursion:
让
是任意集合, 是任意元素 ,并让 是来自的任何函数 到 .然后有一个独特的功能 满足以下两个条款:
对于每一个 在
Let
be any set, be any element of , and let be any function from to . Then there is a unique function satisfying the following two clauses:
for every in
我们可以利用递归定义的原理,用以下两个子句来定义加法:
We can use the principle of recursive definition to define addition with the following two clauses:
请注意,我们正在修复
Note that we are fixing
我们可以使用以下两个子句来定义乘法:
We can proceed to define multiplication using the following two clauses:
我们还可以通过以下方式定义前驱函数
We can also define a predecessor function by
我们可以通过以下方式定义截断减法
We can define truncated subtraction by
利用这些定义和归纳原理,可以证明以下所有恒等式:
With these definitions and the induction principle, one can prove all the following identities:
暗示 implies
我们将在这里完成前五个,并将剩下的作为练习。
We will do the first five here, and leave the remaining ones as exercises.
命题。对于每个自然数
Proposition. For every natural number
证明。通过归纳
Proof. By induction on
命题。对于每一个
Proposition. For every
证明。通过归纳
Proof. By induction on
命题。对于每一个
Proposition. For every
证明。修复
Proof. Fix
使用归纳假设和第二个定义子句进行加法。
using the inductive hypothesis and the second defining clause for addition.
命题。对于每一个
Proposition. For every
证明。通过归纳
Proof. By induction on
使用归纳假设和加法的定义。
using the inductive hypothesis and the definition of addition.
命题。对于每一对自然数
Proposition. For every pair of natural numbers
证明。通过归纳
Proof. By induction on
使用上面的第三个命题。
using the third proposition above.
17.5.自然数的算术¶
17.5. Arithmetic on the Natural Numbers¶
继续上一节的内容,我们可以确定在日常数学中发挥作用的自然数的所有基本属性。我们在这里总结了主要内容:
Continuing as in the last section, we can establish all the basic properties of the natural numbers that play a role in day-to-day mathematics. We summarize the main ones here:
在普通的数学论证或计算中,它们可以在没有明确理由的情况下使用。我们还具有以下属性:
In an ordinary mathematical argument or calculation, they can be used without explicit justification. We also have the following properties:
如果
然后if
then如果
和 然后if
and then
我们可以定义
We can define
(反身性) (reflexivity)如果
和 然后 (及物性)if
and then (transitivity)如果
和 然后 (反对称性)if
and then (antisymmetry)面向所有人
和 , 任何一个 或者 是真的(全部)for all
and , either or is true (totality)如果
然后if
then如果
然后if
then如果
然后if
then如果
然后 或者if
then or
记得第 13 章中的前四项断言
Remember from Chapter 13 that the first four items assert that
那么,像往常一样,我们可以定义
As usual, then, we can define
命题。对于每一个
Proposition. For every
证明。否则,我们就会
Proof. Otherwise, we would have
尤其是
In particular, taking
命题。我们有
Proposition. We have
证明。假设
Proof. Suppose
在另一个方向上,假设
In the other direction, suppose
以类似的方式,我们可以证明
In a similar way, we can show that
永远不会是真的(非反身性) is never true (irreflexivity)如果
和 然后 (及物性)if
and then (transitivity)面向所有人
和 , 任何一个 , 或者 为真(三分法)for all
and , either , or is true (trichotomy)如果
然后if
then如果
和 然后if
and then如果
然后 或者if
then or面向所有人
, 或者for all
, or
前三项意味着
The first three items mean that
证明。我们将利用先前对小于关系的描述来证明其中一些属性。
Proof. We will prove some of these properties using the previous characterization of the less-than relation.
第一个性质很简单:我们知道
The first property is straightforward: we know
对于第二个属性,假设
For the second property, assume
对于第三种情况,我们知道
For the third, we know that either
对于第四个,如果
For the fourth, if
对于第五个,假设
For the fifth, suppose
其余的证明留给读者作为练习。
The rest of the remaining proofs are left as an exercise to the reader.
以下是一些附加属性
Here are some additional properties of
和 不能同时成立(不对称) and cannot both hold (asymmetry)如果
和 然后if
and then如果
和 然后if
and then如果
然后if
then如果
然后if
then如果
然后if
then如果
然后 和if
then and
这些可以从上面的证明中得到。此外,我们刚刚看到的原理集合可用于证明自然数的基本事实,这些事实在非正式数学论证中通常被视为理所当然。
These can be proved from the ones above. Moreover, the collection of principles we have just seen can be used to justify basic facts about the natural numbers, which are again typically taken for granted in informal mathematical arguments.
命题。如果
Proposition. If
证明。如果
Proof. If
命题。如果
Proposition. If
证明。在这个证明中,我们反复使用以下属性:
Proof. In this proof we repeatedly use the property that if
17.6.整数¶
17.6. The Integers¶
自然数是为计数离散量而设计的,但它们有一个令人讨厌的缺点:可以减去
The natural numbers are designed for counting discrete quantities, but they suffer an annoying drawback: it is possible to subtract
在后面的章节中,我们将看到整数可以扩展为有理数、实数和复数,它们各有其用途。然而,对于处理离散量,整数将大有裨益。
We will see in a later chapter that the integers can be extended to the rational numbers, the real numbers, and the complex numbers, each of which serves useful purposes. For dealing with discrete quantities, however, the integers will get us pretty far.
你可以将整数视为由两个自然数组成,一个正数和一个负数,它们共享一个零。相反,一旦我们有了整数,你就可以把自然数视为由非负整数组成,即大于或等于
You can think of the integers as consisting of two copies of the natural numbers, a positive one and a negative one, sharing a common zero. Conversely, once we have the integers, you can think of the natural numbers as consisting of the nonnegative integers, that is, the integers that are greater than or equal to
上一节列举的自然数的大多数性质也适用于整数,但不是全部。例如,不再有
Most of the properties of the natural numbers that were enumerated in the last section hold of the integers as well, but not all. For example, it is no longer the case that
整数与自然数的一个关键区别是,对于每个整数
The key property that the integers enjoy, which sets them apart from the natural numbers, is that for every integer
我们不再能用归纳法证明整数,因为归纳法不涵盖负数。但是,例如,我们可以使用归纳法来证明某个属性适用于每个非负整数。此外,我们知道每个负整数都是正整数的负数。因此,涉及整数的证明通常分为两种情况,一种情况涵盖非负整数,另一种情况涵盖负整数。
We can no longer use proof by induction on the integers, because induction does not cover the negative numbers. But we can use induction to show that a property holds of every nonnegative integer, for example. Moreover, we know that every negative integer is the negation of a positive one. As a result, proofs involving the integers often break down into two cases, where one case covers the nonnegative integers, and the other case covers the negative ones.
17.7.练习¶
17.7. Exercises¶
用符号逻辑的符号写出完全归纳原理。同样用这种方式写出最小元素原理,并使用逻辑运算来证明两者是等价的。
Write the principle of complete induction using the notation of symbolic logic. Also write the least element principle this way, and use logical manipulations to show that the two are equivalent.
证明对于每一个
, 。Show that for every
, .证明对于每一个
, 。Show that for every
, .证明对于每一个
, 。Show that for every
, .给出第 17.3 节中斐波那契数的定义,证明卡西尼恒等式:对于每个
, 提示:在归纳步骤中,写下 作为 。Given the definition of the Fibonacci numbers in Section 17.3, prove Cassini’s identity: for every
, . Hint: in the induction step, write as .证明
。Prove
.证明以下两个恒等式:
提示:使用归纳法
,并同时证明它们。在归纳步骤中,展开 ,同样地 证明第二个方程尤其棘手。使用归纳假设和第一个恒等式来简化左边,并反复展开指数最高的斐波那契数,简化需要证明的方程。(当你算出解法时,写出一个清晰的方程证明,按“正向”方向计算。)Prove the following two identities:
Hint: use induction on
, and prove them both at once. In the induction step, expand , and similarly for . Proving the second equation is especially tricky. Use the inductive hypothesis and the first identity to simplify the left-hand side, and repeatedly unfold the Fibonacci number with the highest index and simplify the equation you need to prove. (When you have worked out a solution, write a clear equational proof, calculating in the ``forward’’ direction.)证明每个自然数都可以写成2 的不同幂的和。对于这个问题,
被算作2的幂。Prove that every natural number can be written as a sum of distinct powers of 2. For this problem,
is counted as power of 2.让
是一组非空整数,满足以下两个属性:如果
, 然后 。如果
,然后每个倍数 是...的一个元素 。
证明存在
,这样 等于 .提示:利用最小元素原理。Let
be a non-empty set of integers such that the following two properties hold:If
, then .If
, then every multiple of is an element of .
Prove that there is some
, such that is equal to the set of multiples of . Hint: use the least element principle.给出一个非正式但详细的证明,对于每个自然数
, ,使用归纳证明、乘法定义和第 17.4 节中证明的定理。Give an informal but detailed proof that for every natural number
, , using a proof by induction, the definition of multiplication, and the theorems proved in Section 17.4.证明乘法对加法有分配作用。换句话说,证明对于自然数
, , 和 , 。您应该使用加法和乘法的定义以及第 17.4 节中证明的事实(但仅此而已)。Show that multiplication distributes over addition. In other words, prove that for natural numbers
, , and , . You should use the definitions of addition and multiplication and facts proved in Section 17.4 (but nothing more).用同样的方法证明乘法符合结合律。你可以使用第 17.4 节和上一个练习中证明的任何事实。
Prove the multiplication is associative, in the same way. You can use any of the facts proved in Section 17.4 and the previous exercise.
证明乘法是可交换的。
Prove that multiplication is commutative.
证明
。Prove
.按照第 17.5 节中的例子,证明如果
是一个自然数, , 然后 是价值观之一 , 或者 。Following the example in Section 17.5, prove that if
is a natural number and , then is one of the values , or .证明如果
和 是自然数, , 然后 ,仅使用第 17.5 节中列出的属性。这很棘手。首先说明
和 大于 ,因此大于或等于 。然后证明如果其中任何一个大于 , 然后 。Prove that if
and are natural numbers and , then , using only properties listed in Section 17.5.This is tricky. First show that
and are greater than , and hence greater than or equal to . Then show that if either one of them is greater than , then .证明第 17.5 节中任何其他未经证明的主张。
Prove any of the other claims in Section 17.5 that were stated without proof.
仅使用第 17.6 节中给出的否定和减法性质,证明以下整数否定和减法性质。
如果
然后 。 。如果
然后 。 。 。如果
然后 。如果
然后 。 。 。如果
然后 。
Prove the following properties of negation and subtraction on the integers, using only the properties of negation and subtraction given in Section 17.6.
If
then . .If
then . . .If
then .If
then . . .If
then .
假设你有一个无限大的棋盘,每个方格上都写有一个自然数。每个方格上的数值是四个相邻方格数值的平均值。请证明棋盘上的所有数值都相等。
Suppose you have an infinite chessboard with a natural number written in each square. The value in each square is the average of the values of the four neighboring squares. Prove that all the values on the chessboard are equal.
证明每个自然数都可以写成不同的非连续斐波那契数的和。例如,
是不允许的,因为 3 和 5 是连续的斐波那契数,但是 是允许的。Prove that every natural number can be written as a sum of distinct non-consecutive Fibonacci numbers. For example,
is not allowed, since 3 and 5 are consecutive Fibonacci numbers, but is allowed.
18.精益中的自然数与归纳法¶
18. The Natural Numbers and Induction in Lean¶
18.1. Lean 中的归纳和递归¶
18.1. Induction and Recursion in Lean¶
zero在 Lean 内部,自然数被定义为通过公理声明的and运算归纳生成的类型succ:
Internally, in Lean, the natural numbers are defined as a type generated inductively from an axiomatically declared zero and succ operation:
inductive nat : Type
| zero : nat
| succ : nat → nat
inductive nat : Type
| zero : nat
| succ : nat → nat
如果您点击按钮将此文本复制到本教科书在线版的编辑器中,您将看到我们用短语和将其包裹起来。这会将定义放入新的“命名空间”,以便定义的标识符为、和,以避免与 Lean 库中的标识符冲突。下面,我们将在示例重复库中定义的对象的多个地方执行此操作。用或输入的 unicode 符号是 的同义词。namespace hiddenend hiddenhidden.nathidden.nat.zerohidden.nat.succℕ\N\natnat
If you click the button that copies this text into the editor in the online version of this textbook, you will see that we wrap it with the phrases namespace hidden and end hidden. This puts the definition into a new “namespace,” so that the identifiers that are defined are hidden.nat, hidden.nat.zero and hidden.nat.succ, to avoid conflicting with the one that is in the Lean library. Below, we will do that in a number of places where our examples duplicate objects defined in the library. The unicode symbol ℕ, entered with \N or \nat, is a synonym for nat.
声明nat为归纳定义类型意味着我们可以通过递归定义函数,并通过归纳证明定理。例如,这是上一章中介绍的前两个递归定义:
Declaring nat as an inductively defined type means that we can define functions by recursion, and prove theorems by induction. For example, these are the first two recursive definitions presented in the last chapter:
open nat
def two_pow : ℕ → ℕ
| 0 := 1
| (succ n) := 2 * two_pow n
def fact : ℕ → ℕ
| 0 := 1
| (succ n) := (succ n) * fact n
open nat
def two_pow : ℕ → ℕ
| 0 := 1
| (succ n) := 2 * two_pow n
def fact : ℕ → ℕ
| 0 := 1
| (succ n) := (succ n) * fact n
加法和数字的定义方式使得 Lean 认为它们本质上是相同的,因此我们可以将这些定义写如下:succ nn + 1
Addition and numerals are defined in such a way that Lean recognizes succ n and n + 1 as essentially the same, so we could instead write these definitions as follows:
def two_pow : ℕ → ℕ
| 0 := 1
| (n + 1) := 2 * two_pow n
def fact : ℕ → ℕ
| 0 := 1
| (n + 1) := (n + 1) * fact n
def two_pow : ℕ → ℕ
| 0 := 1
| (n + 1) := 2 * two_pow n
def fact : ℕ → ℕ
| 0 := 1
| (n + 1) := (n + 1) * fact n
如果我们想要定义函数m^n,我们可以通过修复m,并对第二个参数执行递归来实现:
If we wanted to define the function m^n, we would do that by fixing m, and writing doing the recursion on the second argument:
def pow : ℕ → ℕ → ℕ
| m 0 := 1
| m (n + 1) := m * pow m n
def pow : ℕ → ℕ → ℕ
| m 0 := 1
| m (n + 1) := m * pow m n
nat.pow事实上,这就是Lean 库中自然数幂函数的定义。
In fact, this is how the power function on the natural numbers, nat.pow, is defined in Lean’s library.
Lean 也足够智能,可以解释更复杂的递归形式,例如这个:
Lean is also smart enough to interpret more complicated forms of recursion, like this one:
def fib : ℕ → ℕ
| 0 := 0
| 1 := 1
| (n + 2) := fib (n + 1) + fib n
def fib : ℕ → ℕ
| 0 := 0
| 1 := 1
| (n + 2) := fib (n + 1) + fib n
除了用递归定义函数外,我们还可以用归纳法证明定理。在精益中,递归定义的每个子句都会产生一个新的恒等式。例如,上述定义中的两个子句产生了pow以下两个定理:
In addition to defining functions by recursion, we can prove theorems by induction. In Lean, each clause of a recursive definition results in a new identity. For example, the two clauses in the definition of pow above give rise to the following two theorems:
import data.nat.pow
open nat
example (n : ℕ) : pow n 0 = 1 := rfl
example (m n : ℕ) : pow m (n+1) = m * pow m n := rfl
import data.nat.pow
open nat
example (n : ℕ) : pow n 0 = 1 := rfl
example (m n : ℕ) : pow m (n+1) = m * pow m n := rfl
Lean 定义了指数的常用符号:
Lean defines the usual notation for exponentiation:
import data.nat.pow
open nat
example (n : ℕ) : n^0 = 1 := rfl
example (m n : ℕ) : m^(n+1) = m * m^n := rfl
#check @pow_zero
#check @pow_succ
import data.nat.pow
open nat
example (n : ℕ) : n^0 = 1 := rfl
example (m n : ℕ) : m^(n+1) = m * m^n := rfl
#check @pow_zero
#check @pow_succ
请注意,我们也可以在 定义的第二条子句中使用。当然,我们可以使用乘法的交换性来证明这两个定义是等价的,但是,使用归纳证明,我们也可以仅使用乘法的结合性以及 和 的性质来证明它。这很有用,因为幂函数也经常用于乘法不交换的情况,例如矩阵乘法。该定理可以在 Lean 中证明如下:pow m n * npow1 * m = mm * 1 = m
Notice that we could alternatively have used pow m n * n in the second clause of the definition of pow. Of course, we can prove that the two definitions are equivalent using the commutativity of multiplication, but, using a proof by induction, we can also prove it using only the associativity of multiplication, and the properties 1 * m = m and m * 1 = m. This is useful, because the power function is also often used in situations where multiplication is not commutative, such as with matrix multiplication. The theorem can be proved in Lean as follows:
import data.nat.pow
open nat
example (m n : ℕ) : m^(succ n) = m^n * m :=
nat.rec_on n
(show m^(succ 0) = m^0 * m, from calc
m^(succ 0) = m * m^0 : by rw pow_succ
... = m * 1 : by rw pow_zero
... = m : by rw mul_one
... = 1 * m : by rw one_mul
... = m^0 * m : by rw pow_zero)
(assume n,
assume ih : m^(succ n) = m^n * m,
show m^(succ (succ n)) = m^(succ n) * m, from calc
m^(succ (succ n)) = m * m^(succ n) : by rw pow_succ
... = m * (m^n * m) : by rw ih
... = (m * m^n) * m : by rw mul_assoc
... = m^(succ n) *m : by rw pow_succ)
import data.nat.pow
open nat
example (m n : ℕ) : m^(succ n) = m^n * m :=
nat.rec_on n
(show m^(succ 0) = m^0 * m, from calc
m^(succ 0) = m * m^0 : by rw pow_succ
... = m * 1 : by rw pow_zero
... = m : by rw mul_one
... = 1 * m : by rw one_mul
... = m^0 * m : by rw pow_zero)
(assume n,
assume ih : m^(succ n) = m^n * m,
show m^(succ (succ n)) = m^(succ n) * m, from calc
m^(succ (succ n)) = m * m^(succ n) : by rw pow_succ
... = m * (m^n * m) : by rw ih
... = (m * m^n) * m : by rw mul_assoc
... = m^(succ n) *m : by rw pow_succ)
这是精益中典型的归纳证明。它以短语 开始,然后是基本情况和归纳假设。(该短语允许我们写而不是。这个定理在库中被调用。证明可以使用 来缩短:nat.rec_on nopen natpownat.powpow_succ'rewrite
This is a typical proof by induction in Lean. It begins with the phrase nat.rec_on n, and is followed by the base case and the inductive hypothesis. (The phrase open nat allows us to write pow instead of nat.pow. This theorem is called pow_succ' in the library.The proof can be shortened using rewrite:
import data.nat.pow
open nat
example (m n : ℕ) : m^(succ n) = m^n * m :=
nat.rec_on n
(show m^(succ 0) = m^0 * m, by
rw [pow_succ, pow_zero, mul_one, one_mul])
(assume n,
assume ih : m^(succ n) = m^n * m,
show m^(succ (succ n)) = m^(succ n) * m,
by rw [pow_succ, pow_succ, mul_assoc, ←ih, ←pow_succ])
import data.nat.pow
open nat
example (m n : ℕ) : m^(succ n) = m^n * m :=
nat.rec_on n
(show m^(succ 0) = m^0 * m, by
rw [pow_succ, pow_zero, mul_one, one_mul])
(assume n,
assume ih : m^(succ n) = m^n * m,
show m^(succ (succ n)) = m^(succ n) * m,
by rw [pow_succ, pow_succ, mul_assoc, ←ih, ←pow_succ])
请记住,您可以逐步编写rewrite证明,检查错误消息以确保到目前为止一切正常,并查看 Lean 的进展情况。
Remember that you can write a rewrite proof incrementally, checking the error messages to make sure things are working so far, and to see how far Lean got.
作为归纳证明的另一个例子,这里是恒等式的证明。m^(n + k) = m^n * m^k
As another example of a proof by induction, here is a proof of the identity m^(n + k) = m^n * m^k.
import data.nat.pow
open nat
example (m n k : ℕ) : m^(n + k) = m^n * m^k :=
nat.rec_on k
(show m^(n + 0) = m^n * m^0, from calc
m^(n + 0) = m^n : by rw add_zero
... = m^n * 1 : by rw mul_one
... = m^n * m^0 : by rw pow_zero)
(assume k,
assume ih : m^(n + k) = m^n * m^k,
show m^(n + succ k) = m^n * m^(succ k), from calc
m^(n + succ k) = m^(succ (n + k)) : by rw nat.add_succ
... = m^(n + k) * m : by rw pow_succ'
... = m^n * m^k * m : by rw ih
... = m^n * (m^k * m) : by rw mul_assoc
... = m^n * m^(succ k) : by rw pow_succ')
import data.nat.pow
open nat
example (m n k : ℕ) : m^(n + k) = m^n * m^k :=
nat.rec_on k
(show m^(n + 0) = m^n * m^0, from calc
m^(n + 0) = m^n : by rw add_zero
... = m^n * 1 : by rw mul_one
... = m^n * m^0 : by rw pow_zero)
(assume k,
assume ih : m^(n + k) = m^n * m^k,
show m^(n + succ k) = m^n * m^(succ k), from calc
m^(n + succ k) = m^(succ (n + k)) : by rw nat.add_succ
... = m^(n + k) * m : by rw pow_succ'
... = m^n * m^k * m : by rw ih
... = m^n * (m^k * m) : by rw mul_assoc
... = m^n * m^(succ k) : by rw pow_succ')
注意相同的模式。这一次,我们对 进行归纳k,并且起始情况和归纳步骤都是常规的。该定理pow_add在库中被调用,再一次,凭借一点小聪明,我们可以用 来缩短证明rewrite:
Notice the same pattern. This time, we do induction on k, and the base case and inductive step are routine. The theorem is called pow_add in the library, and once again, with a bit of cleverness, we can shorten the proof with rewrite:
import data.nat.pow
open nat
example (m n k : ℕ) : m^(n + k) = m^n * m^k :=
nat.rec_on k
(show m^(n + 0) = m^n * m^0,
by rw [add_zero, pow_zero, mul_one])
(assume k,
assume ih : m^(n + k) = m^n * m^k,
show m^(n + succ k) = m^n * m^(succ k),
by rw [nat.add_succ, pow_succ', ih, mul_assoc, pow_succ'])
import data.nat.pow
open nat
example (m n k : ℕ) : m^(n + k) = m^n * m^k :=
nat.rec_on k
(show m^(n + 0) = m^n * m^0,
by rw [add_zero, pow_zero, mul_one])
(assume k,
assume ih : m^(n + k) = m^n * m^k,
show m^(n + succ k) = m^n * m^(succ k),
by rw [nat.add_succ, pow_succ', ih, mul_assoc, pow_succ'])
但是,您应该毫不犹豫地使用calc,以使证明更加明确。请记住,您也可以一起使用calc和,使用来构造计算证明,使用来填写每个证明步骤。rewritecalcrewrite
You should not hesitate to use calc, however, to make the proofs more explicit. Remember that you can also use calc and rewrite together, using calc to structure the calculational proof, and using rewrite to fill in each justification step.
18.2.定义 Lean 中的算术运算¶
18.2. Defining the Arithmetic Operations in Lean¶
事实上,加法和乘法在 Lean 中的定义基本与第 17.4 节中所述一致。加法的定义方程符合反身性,但它们也被称为add_zero和add_succ:
In fact, addition and multiplication are defined in Lean essentially as described in Section 17.4. The defining equations for addition hold by reflexivity, but they are also named add_zero and add_succ:
import data.nat.basic
open nat
variables m n : ℕ
example : m + 0 = m := add_zero m
example : m + succ n = succ (m + n) := add_succ m n
import data.nat.basic
open nat
variables m n : ℕ
example : m + 0 = m := add_zero m
example : m + succ n = succ (m + n) := add_succ m n
类似地,我们有前导函数和乘法的定义方程:
Similarly, we have the defining equations for the predecessor function and multiplication:
import data.nat.basic
open nat
#check @pred_zero
#check @pred_succ
#check @mul_zero
#check @mul_succ
import data.nat.basic
open nat
#check @pred_zero
#check @pred_succ
#check @mul_zero
#check @mul_succ
以下是第17.4节中证明的五个命题。
Here are the five propositions proved in Section 17.4.
theorem succ_pred (n : ℕ) : n ≠ 0 → succ (pred n) = n :=
nat.rec_on n
(assume H : 0 ≠ 0,
show succ (pred 0) = 0, from absurd rfl H)
(assume n,
assume ih,
assume H : succ n ≠ 0,
show succ (pred (succ n)) = succ n,
by rewrite pred_succ)
theorem zero_add (n : nat) : 0 + n = n :=
nat.rec_on n
(show 0 + 0 = 0, from rfl)
(assume n,
assume ih : 0 + n = n,
show 0 + succ n = succ n, from
calc
0 + succ n = succ (0 + n) : rfl
... = succ n : by rw ih)
theorem succ_add (m n : nat) : succ m + n = succ (m + n) :=
nat.rec_on n
(show succ m + 0 = succ (m + 0), from rfl)
(assume n,
assume ih : succ m + n = succ (m + n),
show succ m + succ n = succ (m + succ n), from
calc
succ m + succ n = succ (succ m + n) : rfl
... = succ (succ (m + n)) : by rw ih
... = succ (m + succ n) : rfl)
theorem add_assoc (m n k : nat) : m + n + k = m + (n + k) :=
nat.rec_on k
(show m + n + 0 = m + (n + 0), by rw [add_zero, add_zero])
(assume k,
assume ih : m + n + k = m + (n + k),
show m + n + succ k = m + (n + (succ k)), from calc
m + n + succ k = succ (m + n + k) : by rw add_succ
... = succ (m + (n + k)) : by rw ih
... = m + succ (n + k) : by rw ←add_succ
... = m + (n + succ k) : by rw ←add_succ)
theorem add_comm (m n : nat) : m + n = n + m :=
nat.rec_on n
(show m + 0 = 0 + m, by rewrite [add_zero, zero_add])
(assume n,
assume ih : m + n = n + m,
show m + succ n = succ n + m, from calc
m + succ n = succ (m + n) : by rw add_succ
... = succ (n + m) : by rw ih
... = succ n + m : by rw succ_add)
theorem succ_pred (n : ℕ) : n ≠ 0 → succ (pred n) = n :=
nat.rec_on n
(assume H : 0 ≠ 0,
show succ (pred 0) = 0, from absurd rfl H)
(assume n,
assume ih,
assume H : succ n ≠ 0,
show succ (pred (succ n)) = succ n,
by rewrite pred_succ)
theorem zero_add (n : nat) : 0 + n = n :=
nat.rec_on n
(show 0 + 0 = 0, from rfl)
(assume n,
assume ih : 0 + n = n,
show 0 + succ n = succ n, from
calc
0 + succ n = succ (0 + n) : rfl
... = succ n : by rw ih)
theorem succ_add (m n : nat) : succ m + n = succ (m + n) :=
nat.rec_on n
(show succ m + 0 = succ (m + 0), from rfl)
(assume n,
assume ih : succ m + n = succ (m + n),
show succ m + succ n = succ (m + succ n), from
calc
succ m + succ n = succ (succ m + n) : rfl
... = succ (succ (m + n)) : by rw ih
... = succ (m + succ n) : rfl)
theorem add_assoc (m n k : nat) : m + n + k = m + (n + k) :=
nat.rec_on k
(show m + n + 0 = m + (n + 0), by rw [add_zero, add_zero])
(assume k,
assume ih : m + n + k = m + (n + k),
show m + n + succ k = m + (n + (succ k)), from calc
m + n + succ k = succ (m + n + k) : by rw add_succ
... = succ (m + (n + k)) : by rw ih
... = m + succ (n + k) : by rw ←add_succ
... = m + (n + succ k) : by rw ←add_succ)
theorem add_comm (m n : nat) : m + n = n + m :=
nat.rec_on n
(show m + 0 = 0 + m, by rewrite [add_zero, zero_add])
(assume n,
assume ih : m + n = n + m,
show m + succ n = succ n + m, from calc
m + succ n = succ (m + n) : by rw add_succ
... = succ (n + m) : by rw ih
... = succ n + m : by rw succ_add)
18.3.练习¶
18.3. Exercises¶
通过用证明替换每个遗憾,尽可能多地形式化第 17.4 节中的身份。
import data.nat.basic open nat --1.a. example : ∀ m n k : nat, m * (n + k) = m * n + m * k := sorry --1.b. example : ∀ n : nat, 0 * n = 0 := sorry --1.c. example : ∀ n : nat, 1 * n = n := sorry --1.d. example : ∀ m n k : nat, (m * n) * k = m * (n * k) := sorry --1.e. example : ∀ m n : nat, m * n= n * m := sorry
Formalize as many of the identities from Section 17.4 as you can by replacing each sorry with a proof.
import data.nat.basic open nat --1.a. example : ∀ m n k : nat, m * (n + k) = m * n + m * k := sorry --1.b. example : ∀ n : nat, 0 * n = 0 := sorry --1.c. example : ∀ n : nat, 1 * n = n := sorry --1.d. example : ∀ m n k : nat, (m * n) * k = m * (n * k) := sorry --1.e. example : ∀ m n : nat, m * n= n * m := sorry
通过用证明替换每个遗憾,尽可能多地形式化第 17.5 节中的身份。
import data.nat.basic open nat --2.a. example : ∀ m n k : nat, n ≤ m → n + k ≤ m + k := sorry --2.b. example : ∀ m n k : nat, n + k ≤ m + k → n ≤ m := sorry --2.c. example : ∀ m n k : nat, n ≤ m → n * k ≤ m * k := sorry --2.d. example : ∀ m n : nat, m ≥ n → m = n ∨ m ≥ n+1 := sorry --2.e. example : ∀ n : nat, 0 ≤ n := sorry
Formalize as many of the identities from Section 17.5 as you can by replacing each sorry with a proof.
import data.nat.basic open nat --2.a. example : ∀ m n k : nat, n ≤ m → n + k ≤ m + k := sorry --2.b. example : ∀ m n k : nat, n + k ≤ m + k → n ≤ m := sorry --2.c. example : ∀ m n k : nat, n ≤ m → n * k ≤ m * k := sorry --2.d. example : ∀ m n : nat, m ≥ n → m = n ∨ m ≥ n+1 := sorry --2.e. example : ∀ n : nat, 0 ≤ n := sorry
19.初等数论¶
19. Elementary Number Theory¶
在前两章中,我们看到自然数的特点是它们支持归纳证明和递归定义。此外,有了这些组成部分,我们实际上可以定义
In the last two chapters, we saw that the natural numbers are characterized by the fact that they support proof by induction and definition by recursion. Moreover, with these components, we can actually define
自然数和整数是数论的核心组成部分,数论是数学的一个分支,可以追溯到古代。在本章中,我们将讨论该主题的一些基本知识。
The natural numbers and the integers are the central components of number theory, a branch of mathematics dating back to the ancients. In this chapter, we will discuss some of the rudiments of this subject.
19.1.商余数定理¶
19.1. The Quotient-Remainder Theorem¶
我们将在这里使用的整数的一个关键属性是商余数定理:
A key property of the integers that we will use here is the quotient-remainder theorem:
定理。设
Theorem. Let
证明。首先,我们证明以下情况
Proof. First we prove this in the case where
如果
If
此外,由于
Also, since
直观地看,
Intuitively,
命题。如果
Proposition. If
证明。根据假设,我们有
Proof. By assumption, we have
认为
Suppose
但自从
But since
这与以下事实相矛盾:
which contradicts the fact that
19.2.可除性¶
19.2. Divisibility¶
我们可以按如下方式定义整数的可整除性。
We can define divisibility on the integers as follows.
定义。给定两个整数
Definition. Given two integers
我们现在可以证明以下内容:
We can now prove the following:
定理。关系
Theorem. The relation
证明。反身性是直接的,因为
Proof. Reflexivity is immediate, because
对于传递性,假设
For transitivity, suppose
假设
Suppose that
请注意,这意味着如果
Note that this means that if
请参阅练习 1,了解可分性的一些基本性质。例如,对于每个
See Exercise 1 for some basic properties of divisibility. For example, we have that for every
一个整数即使可以被
An integer is even if it is divisible by
定理。如果
Theorem. If
证明。根据商余数定理,我们可以写出
Proof. By the quotient-remainder theorem, we can write
定理。每个序列
Theorem. Every sequence of
证明。用以下公式表示序列中的最大元素:
Proof. Denote the largest element of the sequence by
定义。给定两个整数
Definition. Given two integers
这个最大整数是存在的,因为至少有一个公约数,但只有有限个。至少有一个,因为 1 是任何两个整数的公约数,而有有限个,因为非零数只有有限个公约数。
This largest integer exists, because there is at least one common divisor, but only finitely many. There is at least one, since 1 is a common divisor of any two integers, and there are finitely many, since a nonzero number has only finitely many divisors.
如果
If
两个数的最大公约数始终是自然数,因为 1 始终是两个数的公约数。例如,让我们计算 6 和 28 的最大公约数。6 的正公约数是
The greatest common divisor of two numbers is always a natural number, since 1 is always a common divisor of two numbers. As an example, let us compute the greatest common divisor of 6 and 28. The positive divisors of 6 are
但是,通过列出两个数的所有除数来计算两个数的最大公约数的工作量非常大,因此我们现在将考虑一种更有效地计算最大公约数的方法。
However, computing the greatest common divisor of two numbers by listing all the divisors of both numbers is a lot of work, so we will now consider a method to compute the greatest common divisor more efficiently.
引理对于所有整数
Lemma. For all integers
证明。设
Proof. Let
在另一种情况下,我们首先证明
In the other case we first show that the set of common divisors of
由于公约数的集合相同,因此每个集合中的最大元素也相同,因此
Since the sets of common divisors are the same, the largest element in each set is also the same, hence
引理对于所有整数
Lemma. For all integers
证明。每个数字都是 0 的除数,因此最大公约数是
Proof. Every number is a divisor of 0, hence the greatest common divisor of
这两个引理为我们提供了一种快速计算两个数字的最大公约数的方法。这称为欧几里得算法。假设我们要计算
These two lemmas give us a quick way to compute the greatest common divisor of two numbers. This is called the Euclidean Algorithm. Suppose we want to compute
我们让
和 。We let
and .鉴于
和 我们计算 作为的剩余部分 当除以 。Given
and we compute as the remainder of of when divided by .一次
,我们停下来, 。Once
, we stop, and .
这是可行的,因为根据上面的引理,我们有
This works, because by the lemmas above, we have
例如,假设我们要计算 1311 和 5757 的最大公约数。我们计算以下余数:
For example, suppose we want to compute the greatest common divisor of 1311 and 5757. We compute the following remainders:
因此
Hence
这是关于最大公约数的一个重要结果。由于历史原因,它被称为“引理”。
Here is an important result about greatest common divisors. It is only called a “lemma” for historical reasons.
定理(Bézout 引理)。设
Theorem (Bézout’s Lemma). Let
证明。我们计算
Proof. We compute
这就完成了归纳。特别是,
This completes the induction. In particular,
替代证明。我们可以假设
Alternative proof. We can assume
让
Let
我们有
We have
和
with
推论。如果
Corollary. If
证明。根据 Bézout 引理,有
Proof. By Bézout’s Lemma, there are
特别令人感兴趣的是,除了 1 和
Of special interest are pairs of integers which have no divisors in common, except 1 and
定义。两个整数
Definition. Two integers
命题。设
Proposition. Let
证明。根据 Bézout 引理,有
Proof. By Bézout’s Lemma, there are
19.3.素数¶
19.3. Prime Numbers¶
在本节中,我们将考虑素数的性质。
In this section we consider properties of prime numbers.
定义。一个整数
Definition. An integer
素数的等效定义是具有恰好 2 个正因数的正数。
An equivalent definition of a prime number is a positive number with exactly 2 positive divisors.
回想一下第 17 章,每个大于 1 的自然数都可以写成素数的乘积。具体来说,每个大于 1 的自然数都可以被某个素数整除。
Recall from Chapter 17 that every natural number greater than 1 can be written as the product of primes. In particular, ever natural number greater than 1 is divisible by some prime number.
我们现在证明有关素数的一些其他性质。
We now prove some other properties about prime numbers.
定理。素数有无穷多个。
Theorem. There are infinitely many primes.
证明。为了矛盾,假设素数只有有限个
Proof. Suppose for the sake of contradiction that there are only finitely many primes
引理。如果
Lemma. If
证明。设
Proof. Let
命题。如果
Proposition. If
证明。假设
Proof. Suppose that
本节的最后一个结果表明,素数是正整数乘法的“构建块”:所有其他整数都可以以本质上独特的方式写成素数的乘积。
The last result in this section captures that the primes are the “building blocks” of the positive integers for multiplication: all other integers can be written as a product of primes in an essentially unique way.
定理(算术基本定理)。设
Theorem (Fundamental Theorem of Arithmetic). Let
备注: 1 可以写成零个素数的乘积。空乘积定义为 1。
Remark. 1 can be written as the product of zero prime numbers. The empty product is defined to be 1.
证明。我们已经看到,每个数字都可以写成素数的乘积,所以我们只需要证明重新排序之前的唯一性。假设这不成立,根据最小元素原理,让
Proof. We have already seen that every number can be written as the product of primes, so we only need to prove the uniqueness up to reordering. Suppose this is not true, and by the least element principle, let
由于 1 只能写成素数的乘积,即空乘积,因此有
Since 1 can be written as product of primes only as an empty product, we have
现在我们有
Now we have
19.4.模运算¶
19.4. Modular Arithmetic¶
在13.3 节讨论等价关系时,我们考虑了整数模等价关系的例子。这有时被认为是“时钟算术”。假设你有一个没有分针的 12 小时制时钟,所以它只有一根时针,可以指向 12、1、2、3、4、5、6、7、8、9、10、11 点,然后再次回到 12 点。我们可以用这个时钟做算术。
In the discussion of equivalence relations in Section 13.3 we considered the example of the relation of modular equivalence on the integers. This is sometimes thought of as “clock arithmetic.” Suppose you have a 12-hour clock without a minute hand, so it only has an hour hand which can point to the hours 12, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 and then it wraps to 12 again. We can do arithmetic with this clock.
如果指针当前指向 10,那么 5 小时后它将指向 3。
If the hand currently points to 10, then 5 hours later it will point to 3.
如果指针指向 7,那么在此之前的 23 小时,它指向 8。
If the hand points to 7, then 23 hours before that, it pointed to 8.
如果指针指向 9 点,而我们工作了 8 个小时,那么当我们完成工作时,指针将指向 5 点。如果我们工作的时间是 9 点的两倍,那么指针将指向 1 点。
If the hand points to 9, and we work for a 8 hours, then when we are done the hand will point to 5. If we worked twice as long, starting at 9, the hand will point to 1.
我们希望用数学符号来写这些语句,这样我们就可以更容易地推理它们。我们不能写
We want to write these statements using mathematical notation, so that we can reason about them more easily. We cannot write
这里还有更多示例:
Here are some more examples:
最后一个例子表明我们也可以使用负数。
The last example shows that we can use negative numbers as well.
我们现在给出一个精确的定义。
We now give a precise definition.
定义。对于整数
Definition. For integers
通常我们只在模量
Typically we only use this definition when the modulus
定理。模同余
Theorem. Congruence modulo
证明。我们必须证明模同余
Proof. We have to show that congruence modulo
它是反身性的,因为
It is reflexive, because
为了证明它是对称的,假设
To show that it is symmetric, suppose that
为了证明它是传递的,假设
To show that it is transitive, suppose that
这个定理证明了我们上面使用的“链式”符号的合理性,我们这样写
This theorem justifies the “chaining” notation we used above when we wrote
定理。假设
Theorem. Suppose that
此外,如果
Moreover, if
证明。我们知道
Proof. We know that
对于第二条陈述,我们想证明
For the second statement, we want to show that
最后一句话是归纳
The last statement follows by induction on
该定理对于进行模计算很有用
This theorem is useful for carrying out computations modulo
假设我们要计算
模 12。我们知道 和 , 所以Suppose we want to compute
modulo 12. We know that and , so假设我们要计算
模 10。我们知道 和 , 因此 。Suppose we want to compute
modulo 10. We know that and , hence .假设我们想知道
。请注意,数字的最后一位 符合于 模 10,所以我们可以计算 . 因此 为 1。Suppose we want to know the last digit of
. Notice that the last digit of a number is congruent to modulo 10, so we can just compute . So the last digit of is 1.
警告。您无法使用模块化算法完成所有预期的计算:
Warning. You cannot do all computations you might expect with modular arithmetic:
模数运算不考虑除法。例如
,但我们不能把等式的两边同时除以 2,因为 。Modular arithmetic does not respect division. For example
, but we cannot divide both sides of the equation by 2, because .指数也不遵循模运算。例如
, 但 . 要查看此内容: , 但 。Exponents also do not respect modular arithmetic. For example
, but . To see this: , but .
回想一下商余数定理:如果
Recall the quotient-remainder theorem: if
命题。对于每个整数
Proposition. For every integer
证明。将这个问题转化为模运算,我们必须证明
Proof. Translating this problem to modular arithmetic, we have to show that
命题。对于所有整数
Proposition. For all integers
证明。我们首先计算模 4 的平方。我们计算
Proof. We first compute the squares modulo 4. We compute
因为每个数都与 0、1、2 或 3 模 4 一致,所以我们知道每个平方数都与 0 或 1 模 4 一致。这意味着
Since every number is congruent to 0, 1, 2 or 3 modulo 4, we know that every square is congruent to 0 or 1 modulo 4. This means that there are only four possibilities for
回想一下,我们警告过你关于模运算中的除法。这并不总是有效,但通常有效。例如,假设我们想解决
Recall that we warned you about dividing in modular arithmetic. This doesn’t always work, but often it does. For example, suppose we want to solve
定义。让
Definition. Let
例如,3 是 5 模 7 的乘法逆元,因为
For example, 3 is a multiplicative inverse of 5 modulo 7, since
引理
Lemma Let
证明。假设
Proof. Suppose that
命题。设
Proposition. Let
证明。假设
Proof. Suppose
另一方面,假设
On the other hand, suppose that
请注意,如果
Note that if
19.5.正方形的性质¶
19.5. Properties of Squares¶
自古以来,数学家们就对哪些整数可以写成两个平方和的问题很感兴趣。例如,我们可以写成
Mathematicians from ancient times have been interested in the question as to which integers can be written as a sum of two squares. For example, we can write
一开始,你可能会怀疑这是否只是巧合。下面将提供证据来证明事实并非如此。
At first, one might wonder whether this is just a coincidence. The following provides a proof of the fact that it is not.
定理。设
Theorem. Let
证明。假设
Proof. Suppose
为了说明这一点,请注意,一方面我们有
To show this, notice that on the one hand we have
另一方面,我们有
On the other hand, we have
就加数的顺序而言,两个右边是相同的。
Up to the order of summands, the two right-hand sides are the same.
考虑素数,
Consider the prime numbers,
我们现在将证明
We will now prove that
定理。没有整数
Theorem. There are no integers
证明。假设
Proof. Suppose that
因此没有整数
Hence there are no integers
19.6.练习¶
19.6. Exercises¶
证明以下关于可整除性的性质(对于任何整数
, 和 ):如果
和 然后 和 。如果
然后 。 ;如果
然后 。如果
然后是语句 和 是等效的。如果
和 然后 。
Prove the following properties about divisibility (for any integers
, and ):If
and then and .If
then . ;If
then .If
then the statements and are equivalent.If
and then .
证明如果
, , 和 , 然后 (提示:这有助于表明,无论何时 , , 和 , 然后 。Prove that if
, , and , then . (Hint: it helps to show that whenever , , and , then .)证明对于任何整数
, 除以 4 后余数为 0 或 1。得出以下结论 永远不能被 4 整除。Prove that for any integer
, leaves a remainder of 0 or 1 when you divide it by 4. Conclude that is never divisible by 4.证明如果
很奇怪, 可以被 8 整除。Prove that if
is odd, is divisible by 8.证明如果
和 是奇数,那么 是偶数但不能被 4 整除。Prove that if
and are odd, then is even but not divisible by 4.如果两个整数都是偶数或都是奇数,则称它们“具有相同的奇偶性”。证明如果
和 是任意两个整数,那么 和 具有相同的奇偶校验。Say that two integers “have the same parity” if they are both even or both odd. Prove that if
and are any two integers, then and have the same parity.将 11160 写为质数的乘积。
Write 11160 as a product of primes.
列出 42 和 198 的所有因数,然后通过查看两个列表中的最大数字来找到最大公约数。还使用欧几里得算法计算数字的最大公约数。
List all the divisors of 42 and 198, and find the greatest common divisor by looking at the largest number in both lists. Also compute the greatest common divisor of the numbers by the Euclidean Algorithm.
计算
, 和Compute
, and通过感应显示
对于每一对整数 和 , 分裂 .(提示:在归纳步骤中,写下 作为 。Show by induction on
that for every pair of integers and , divides . (Hint: in the induction step, write as .)计算
. 使用它来计算 。Compute
. Use this to compute .查找最后一位数字
。您还能找到这个数字的最后两位数字吗?Find the last digit of
. Can you also find the last two digits of this number?证明
可以被 7 整除。Prove that
is divisible by 7.检查以下乘法逆元是否存在,如果存在,则找出它们。
5 模 7 的乘法逆元
17 模 21 的乘法逆元
4 模 14 的乘法逆元
的乘法逆元
模 9
Check whether the following multiplicative inverses exist, and if so, find them.
the multiplicative inverse of 5 modulo 7
the multiplicative inverse of 17 modulo 21
the multiplicative inverse of 4 modulo 14
the multiplicative inverse of
modulo 9
查找所有整数
使得 。Find all integers
such that .证明对于每个整数
号码 与 0 或 1 模 5 一致。提示:为了简化计算,使用 。Show that for every integer
the number is congruent to 0 or 1 modulo 5. Hint: to simplify the computation, use that .证明方程
在整数中没有解。(提示:使用上一个练习。)Prove that the equation
has no solutions in the integers. (Hint: use the previous exercise.)认为
是一个素数, . 证明如果 然后 。Suppose
is a prime number such that . Show that if then .让
, 和 给定整数。使用 Bézout 引理证明方程 有整数解 和 当且仅当 。Let
, and be given integers. Use Bézout’s Lemma to prove that the equation has a solution for integers and if and only if .假设
和 并让 . 证明 . 得出以下结论:对于任何两个整数 和 最大公约数 数字 和 是互质的。Suppose that
and and let . Prove that . Conclude that for any two integers and with greatest common divisor the numbers and are coprime.
20.组合学¶
20. Combinatorics¶
组合学是一门无需计数的计数艺术。确定给定集合中有多少东西是一项基本的数学任务,当集合很大时,逐个计算元素可能会很繁琐或不可行。此外,当用变化的参数(例如自然数)来描述集合时,
Combinatorics is the art of counting without counting. It is a fundamental mathematical task to determine how many things there are in a given collection, and when the collection is large, it can be tedious or infeasible to count the elements individually. Moreover, when the collection is described in terms of a changing parameter (say, a natural number,
20.1.有限集和基数¶
20.1. Finite Sets and Cardinality¶
对于每个自然数来说,这都是有帮助的
It will be helpful, for every natural number
我们使用了相同的符号,
We used the same notation,
一套
A set
假设两者都有双射
Suppose there are bijections from both
因此,为了矛盾,假设
So suppose, for the sake of contradiction,
定理。对于任何自然数
Theorem. For any natural number
证明。通过归纳
Proof. By induction on
假设断言对
So suppose the claim is true for
在第一种情况下,假设
In the first case, suppose
在第二种情况下,
In the second case, there is some
否则,定义
Otherwise, define
换句话说,我们映射
In other words, we map
这个定理被称为“鸽巢原理”。这意味着如果
This theorem is known as the “pigeonhole principle.” It implies that if
我们现在引入符号
We now introduce the notation
20.2.计数原则¶
20.2. Counting Principles¶
这是一个基本的计数原理。
Here is a basic counting principle.
定理。设
Theorem. Let
证明。假设
Proof. Suppose
看到这一点
To see that
不难证明
It is not hard to show that
证明只是阐明了我们的基本直觉:如果你想列出
The proof only spells out our basic intuitions: if you want to list all of the elements of
这是另一个基本的计数原则:
Here is another basic counting principle:
定理。设
Theorem. Let
注意,这次我们计算的是有序对的数量
Notice that this time we are counting the number of ordered pairs
并且此并集内的两组集合是不相交的。
and the two sets in this union are disjoint.
正如我们有联盟的概念
Just as we have notions of union
定理。设
Theorem. Let
如果每对集合
, 是不相交的,那么 。If each pair of sets
, are disjoint, then . 。 .
证明。通过归纳
Proof. By induction on
我们已经可以利用这些原理进行基本的计算。
We can already use these principles to carry out basic calculations.
示例。一家餐厅的甜点菜单上有四种口味的冰淇淋、两种蛋糕和三种馅饼。有多少种甜点可供选择?
Example. The dessert menu at a restaurant has four flavors of ice cream, two kinds of cake, and three kinds of pie. How many dessert choices are there?
解决方案。
Solution.
示例。一家餐厅的菜单上有 6 种开胃菜、7 种主菜和 5 种甜点。那么三道菜的晚餐有多少种选择?
Example. The menu at a diner has 6 choices of appetizers, 7 choices of entrée, and 5 choices of dessert. How many choices of three-course dinners are there?
解决方案。三道菜的晚餐是由开胃菜、主菜和甜点组成的三重菜。因此
Solution. A three-course dinner is a triple consisting of an appetizer, an entrée, and a dessert. There are therefore
当所有集合的大小相同时,会出现上述计数原理的一个特殊情况。如果
A special case of the previous counting principles arises when all the sets have the same size. If
示例。一副扑克牌有四种花色(方块、红心、黑桃和梅花),每种花色有 13 张牌,总共
Example. A deck of playing cards has four suits (diamonds, hearts, spades, and clubs) and 13 cards in each suit, for a total of
示例。长度为
Example. A binary string of length
所以有
so there are
还有另一条显而易见的计算原则:如果
There is another counting principle that is almost too obvious to mention: if
例子。考虑
Example. Consider the power set of
20.3.有序选择¶
20.3. Ordered Selections¶
让
Let
在问题的第一个变体中,你可以重复选择。例如,如果你从 31 种冰淇淋口味列表中选择 3 种口味,你可以选择“巧克力、香草、巧克力”。这被称为重复有序选择。如果你正在做
In the first variant of the problem, you are allowed to repeat a choice. For example, if you are choosing 3 flavors from a list of 31 ice cream flavors, you can choose “chocolate, vanilla, chocolate.” This is known as ordered selection with repetition. If you are making
定理。设
Theorem. Let
示例。使用字母表中的二十六个字母可以形成多少个三个字母的字符串(例如“xyz”、“qqa”……)?
Example. How many three-letter strings (like “xyz,” “qqa,” …) can be formed using the twenty-six letters of the alphabet?
解决方案。我们必须从 26 个元素中做出 3 个选择,总共
Solution. We have to make three selections from a set of 26 elements, for a total of
假设我们想让
Suppose instead we wish to make
让我们试着用数学术语来阐述这个问题。我们可以想像一下有序选择
Let us try to frame the problem in mathematical terms. We can think of an ordered selection of
定理。设
Theorem. Let
证明。利用归纳法
Proof. Using induction on
定理。设
Theorem. Let
证明。这只是对前一个定理的重述,其中
Proof. This is just a restatement of the previous theorem, where
如果
If
更一般的情况是,我们只选择
The more general case where we are choosing only
20.4.组合和二项式系数¶
20.4. Combinations and Binomial Coefficients¶
在上一节中,我们计算了博物馆从其储藏室的 26 幅画作中挑选三幅画作,将它们排列在一面墙上的方式数量。根据上一节中的最后观察,我们可以将这个数字写成
In the last section, we calculated the number of ways in which a museum could arrange three paintings along a wall, chosen from among 26 paintings in its storeroom. By the final observation in the previous section, we can write this number as
假设现在我们想计算博物馆从其储藏室中挑选三幅画作进行展出的方式数量,我们不关心顺序。换句话说,如果
Suppose now we want to calculate the number of ways that a museum can choose three paintings from its storeroom to put on display, where we do not care about the order. In other words, if
问题是,每组三幅画会被计算多次。事实上,每幅画会被计算六次:
The problem is that each set of three paintings will be counted multiple times. In fact, each one will be counted six times: there are
这些数字没什么特别的
There is nothing special about the numbers
事实上,用更数学的术语来描述这种情况是相当容易的。如果
In fact, describing the situation in more mathematical terms is quite easy to do. If
定理。设
Theorem. Let
证明。设
Proof. Let
根据计数原理,我们有
By the counting principles, we have
所以我们有
so we have
例如。某人要去度假,想从衣柜里的十套衣服中挑选三套装进行李箱。他们有多少种选择?
Example. Someone is going on vacation and wants to choose three outfits from ten in their closet to pack in their suitcase. How many choices do they have?
解决方案。
Solution.
无序选择的数量
The number of unordered selections of
这是该函数的一个重要属性。
Here is one important property of this function.
定理。对于每一个
Theorem. For every
证明。这是一个简单的计算:
Proof. This is an easy calculation:
但从组合解释中也很容易看出:选择
But it is also easy to see from the combinatorial interpretation: choosing
这是另一个重要属性。
Here is another important property.
定理。对于每一个
Theorem. For every
证明。理解这个定理的一种方法是从组合解释的角度。假设你想选择
Proof. One way to understand this theorem is in terms of the combinatorial interpretation. Suppose you want to choose
该定理也可以通过直接计算来证明。我们可以将等式的左边表示如下:
The theorem can also be proved by direct calculation. We can express the left-hand side of the equation as follows:
类似地,我们可以简化右边:
Similarly, we can simplify the right-hand side:
因此左边和右边相等。
Thus the left-hand side and the right-hand side are equal.
对于每一个
For every
具体来说,如果我们从
Specifically, if we start counting at
也存在着
There is also a connection between
因此,价值观
For that reason, the values
被称为二项式定理。
is known as the binomial theorem.
有几种方法可以说明为什么这个定理成立。一种方法是展开多项式,
There are a couple of ways of seeing why this theorem holds. One is to expand the polynomial,
并注意到该项的系数
and notice that the coefficient of the term
最后,我们考虑了有重复和无重复的有序选择,以及无重复的无序选择。那么有重复的无序选择呢?换句话说,给定一个集合
Finally, we have considered ordered selections with and without repetitions, and unordered selections without repetitions. What about unordered selections with repetitions? In other words, given a set
无序选择的数量
The number of unordered selections of
练习中概述了这一点的证明。
A proof of this is outlined in the exercises.
20.5.包含-排斥原理¶
20.5. The Inclusion-Exclusion Principle¶
让
Let
同样地,我们有
Similarly, we have
如果我们画一个维恩图,这是有道理的:计算
If we draw a Venn diagram, this makes sense: to count the elements in
当有三个集合时会发生什么?计算
What happen when there are three sets? To compute
这可以推广到任意数量的集合。为了说明一般结果,假设集合被编号
This generalizes to any number of sets. To state the general result, suppose the sets are numbered
如果你雄心勃勃,欢迎你尝试证明这一点。以下示例说明了它的用法:
You are invited to try proving this as an exercise, if you are ambitious. The following example illustrates its use:
示例。在一组大学新生中,有 30 人选修逻辑,25 人选修历史,20 人选修法语。此外,有 11 人选修逻辑和历史,10 人选修逻辑和法语,7 人选修历史和法语,3 人选修全部三门课程。有多少名学生选修了至少一门课程?
Example. Among a group of college Freshmen, 30 are taking Logic, 25 are taking History, and 20 are taking French. Moreover, 11 are taking Logic and History, 10 are taking Logic and French, 7 are taking History and French, and 3 are taking all three. How many students are taking at least one of the three classes?
解决方案。让
Solution. Letting
20.6.练习¶
20.6. Exercises¶
假设在聚会上,每两个人要么互相认识,要么不认识。换句话说,“
知道 ”是对称的。另外,让我们忽略我们是否总是通过将注意力限制在不同人之间的关系上来了解自己的复杂问题;换句话说,对于这个问题,取“ 知道 ” 也是不反身的。使用鸽巢原理(和额外的见解)来表明一定有两个人认识完全相同数量的人。
Suppose that, at a party, every two people either know each other or don’t. In other words, “
knows ” is symmetric. Also, let us ignore the complex question of whether we always know ourselves by restricting attention to the relation between distinct people; in other words, for this problem, take “ knows ” to be irreflexive as well.Use the pigeonhole principle (and an additional insight) to show that there must be two people who know exactly the same number of people.
证明在任意一组
整数,其中两个是模等价的 。Show that in any set of
integers, two of them are equivalent modulo .详细阐述第 20.2 节中第二计数原理的证明。
Spell out in detail a proof of the second counting principle in Section 20.2.
一家冰淇淋店有 31 种口味的冰淇淋。
如果我们关心顺序并且允许重复,确定可能有多少种三种口味的冰淇淋甜筒。(因此,从下到上选择巧克力-巧克力-香草冰淇淋球与选择巧克力-香草-巧克力冰淇淋球是不同的。)
如果我们关心顺序,但不允许重复,确定有多少种三种口味的冰淇淋蛋筒。
如果我们不关心顺序,但不允许重复,则确定有多少种三种口味的冰淇淋蛋筒。
An ice cream parlor has 31 flavors of ice cream.
Determine how many three-flavor ice-cream cones are possible, if we care about the order and repetitions are allowed. (So choosing chocolate-chocolate-vanilla scoops, from bottom to top, is different from choosing chocolate-vanilla-chocolate.)
Determine how many three flavor ice-cream cones are possible, if we care about the order, but repetitions are not allowed.
Determine how many three flavor ice-cream cones are possible, if we don’t care about the order, but repetitions are not allowed.
一个有 10 人的俱乐部需要选举一名主席、一名副主席和一名秘书。有多少种选择:
如果一个人不能担任多个职务?
是否任何人都可以担任任意数量的这些职务?
是否有人可以担任最多两个职务?
如果总统不能担任其他职务,但副总统和秘书可能是或不是同一个人?
A club of 10 people has to elect a president, vice president, and secretary. How many possibilities are there:
if no person can hold more than one office?
if anyone can hold any number of those offices?
if anyone can hold up to two offices?
if the president cannot hold another office, but the vice president and secretary may or may not be the same person?
如果可以使用任意 7 位数字,那么 7 位电话号码有多少个?如果第一位数字不能为 0,那么有多少个?
How many 7 digit phone numbers are there, if any 7 digits can be used? How many are there if the first digit cannot be 0?
幼儿园一个班有 20 名学生,其中有两名是双胞胎。有多少种排学生的方法,可以让双胞胎站在一起?
In a class of 20 kindergarten students, two are twins. How many ways are there of lining up the students, so that the twins are standing together?
一位女士的书架上有 8 本谋杀悬疑小说,她想在度假时带上其中三本。她有多少种方法可以做到这一点?
A woman has 8 murder mysteries sitting on her shelf, and wants to take three of them on a vacation. How many ways can she do this?
在扑克中,“满堂红”是指一手牌中有三张同等级的牌和两张其他等级的牌(例如三张 K 和两张 5)。确定一副普通的 52 张牌可以组成满堂红的数量。
In poker, a “full house” is a hand with three of one rank and two of another (for example, three kings and two fives). Determine the number of full houses that can be formed from an ordinary deck of 52 cards.
我们在第 20.4 节中看到
更换
经过 ,无论何时 ,我们有以此证明,通过归纳
,对于每一个 ,如果 是任意的集合 元素, 是子集的数量 和 元素。We saw in Section 20.4 that
Replacing
by , whenever , we haveUse this to show, by induction on
, that for every , that if is any set of elements, is the number of subsets of with elements.MISSISSIPPI 这个词中字母有多少种不同的排列?
(提示:这很棘手。首先,假设所有的 S、I 和 P 都被涂上了不同的颜色。然后确定字母有多少种不同的排列。在没有区分颜色的情况下,确定每种配置在第一次计数中出现的次数,然后除以该数字。)
How many distinct arrangements are there of the letters in the word MISSISSIPPI?
(Hint: this is tricky. First, suppose all the S’s, I’s, and P’s were painted different colors. Then determine how many distinct arrangements of the letters there would be. In the absence of distinguishing colors, determine how many times each configuration appeared in the first count, and divide by that number.)
证明容斥原理。
Prove the inclusion-exclusion principle.
使用包含-排除原理确定可以被 2、3 或 5 整除的小于 100 的整数的数量。
Use the inclusion-exclusion principle to determine the number of integers less than 100 that are divisible by 2, 3, or 5.
显示无序选择的数量
来自 -元素集是 。提示:考虑
我们需要选择一些数字 由 0 组成,某个数字 1,等等,这样 假设我们为每个这样的元组分配以下二进制序列:我们写下 0,然后是 1,然后 0,然后是 1,然后 0,依此类推。结果是一个长度为 正是 1,这样的二进制序列就这样从一个唯一的元组产生。Show that the number of unordered selections of
elements from an -element set is .Hint: consider
. We need to choose some number of 0’s, some number of 1’s, and so on, so that . Suppose we assign to each such tuple a the following binary sequence: we write down 0’s, then a 1, then 0’s, then a 1, then 0’s, and so on. The result is a binary sequence of length with exactly 1’s, and such binary sequence arises from a unique tuple in this way.
21.实数¶
21. The Real Numbers¶
21.1.数字系统¶
21.1. The Number Systems¶
我们已经接触过一些基本的数字系统:自然数,
We have already come across some of the fundamental number systems: the natural numbers,
整数满足以下所有属性:
The integers satisfy all of the following properties:
加法具有结合律和交换律。
Addition is associative and commutative.
有一个附加恒等式,
,并且每个元素 有一个加法逆元, 。There is an additive identity,
, and every element has an additive inverse, .乘法是结合律和交换律。
Multiplication is associative and commutative.
有一个乘法恒等式,
。There is a multiplicative identity,
.乘法分配于加法:对于每个
, , 和 ,我们有 。Multiplication distributes over addition: for every
, , and , we have .排序
是一个全序。The ordering
is a total order.对于任意元素
, , 和 , 如果 然后 。For any elements
, , and , if then .对于任意元素
和 , 如果 和 然后 。For any elements
and , if and then .
前五条规定
The first five clauses say that with
每个非零元素都有一个乘法逆元,
。Every nonzero element has a multiplicative inverse,
.
这使得它们成为有序字段的实例。
This makes them an instance of an ordered field.
值得一提的是,一旦我们有了自然数,就可以使用你已经见过的集合论构造来构造整数和有理数。例如,我们可以将一个整数视为一对
It is worth knowing that once we have the natural numbers, it is possible to construct the integers and rational numbers, using set-theoretic constructions you have already seen. For example, we can take an integer to be a pair
这种构造的副作用是自然数本身不是整数;例如,我们必须区分自然数
This construction has the side effect that the natural numbers themselves are not integers; for example, we have to distinguish between the natural number
我们可以对有理数做同样的事情,将它们定义为对的集合
We can do the same for the rationals, defining them to be the set of pairs
在下一节中,我们将定义一种更复杂的方法,这种方法将扩展到实数的定义。在后面的章节中,我们将展示如何从集合论的公理中构造自然数。这表明我们可以从下往上构建所有的数字系统。
In the next section, we will define a more sophisticated approach, one which will scale to a definition of the real numbers. And in a later chapter, we will show how to construct the natural numbers from the axioms of set theory. This shows that we can construct all the number systems from the bottom up.
但首先,让我们暂停一下,考虑一下为什么需要实数。我们已经看到
But first, let us pause for a moment to consider why the real numbers are needed. We have seen that
你可能习惯将实数视为(潜在的)无限小数:例如,
You may be used to thinking of real numbers as (potentially) infinite decimals: for example,
事实上,我们将实数或多或少地定义为有理近似的序列。但我们必须面对这样一个事实,例如,有很多方法可以近似 2 的平方根。例如,我们也可以从上面来处理它,
In fact, we will define the real numbers, more or less, to be such sequences of rational approximations. But we will have to deal with the fact that, for example, there are lots of ways of approximating the square root of two. For example, we can just as well approach it from above,
21.2.商构造¶
21.2. Quotient Constructions¶
让
Let
给定任意集合
Given any set
例如,如果我们考虑整数
For example, if we consider the integers
将定义在集合上的函数转换为定义在该集合的商上的函数的一般策略由以下定理给出。
This general strategy for transferring a function defined on a set to a function defined on a quotient of that set is given by the following theorem.
定理。设
Theorem. Let
证明。我们已经定义了
Proof. We have defined the value of
数学家经常“定义”
Mathematicians often “define”
要构造整数,首先
To construct the integers, start with
定义。定义关系
Definition. Define the relation
主张。
Proposition.
证明。对于反身性,显然
Proof. For reflexivity, it is clear that
为了对称,假设
For symmetry, suppose
对于传递性,假设
For transitivity, suppose
减法
Subtracting
现在我们可以定义整数为
We can now define the integers to be
命题。如果
Proposition. If
证明。第一个等价意味着
Proof. The first equivalence means
每个自然数
Every natural number
我们可以继续以这种方式定义整数的乘法和排序,并证明它们具有所需的属性。我们也可以对有理数进行类似的构造。在这里,我们将从集合开始
We could go on this way to define multiplication and the ordering on the integers, and prove that they have the desired properties. We could also carry out a similar construction for the rational numbers. Here, we would start with the set
21.3.构造实数¶
21.3. Constructing the Real Numbers¶
我们面临的问题是
The problem we face is that the sequence
定义。有理数序列
Definition. A sequence of rational numbers
粗略地说,柯西序列是元素变得任意接近的序列,不仅接近其后继元素,而且接近所有后续元素。在数学中,通常使用
Roughly speaking, a Cauchy sequence is one where the elements become arbitrarily close, not just to their successors but to all following elements. It is common in mathematics to use
柯西序列可用于描述这些有理数之间的差距,但如上所述,许多柯西序列可用于描述相同的差距。在这个阶段,说它们“接近同一点”有点误导,因为它们没有接近的有理点;更准确的说法是,这些序列最终会变得任意接近。
Cauchy sequences can be used to describe these gaps in the rationals, but, as noted above, many Cauchy sequences can be used to describe the same gap. At this stage, it is slightly misleading to say that they “approach the same point,” since there is no rational point that they approach; a more precise statement is that the sequences eventually become arbitrarily close.
定义。两个柯西序列
Definition. Two Cauchy sequences
主张。
Proposition.
证明。自反性和对称性很容易,所以让我们证明传递性。假设
Proof. Reflexivity and symmetry are easy, so let us prove transitivity. Suppose
按要求。
as required.
请注意,证明中使用了三角不等式,该不等式指出,对于任何有理数
Notice that the proof uses the triangle inequality, which states for any rational numbers
定理。设
Theorem. Let
证明。根据绝对值的定义,足以证明
Proof. By the definition of absolute value, it suffices to show that
在上面的定理中,如果我们让
In the theorem above, if we let
我们现在让
We now let
拥有一套
Having the set
幸运的是,在柯西序列上定义加法、减法和乘法很容易。如果
Luckily, it is easy to define addition, subtraction, and multiplication on Cauchy sequences. If
我们可以识别每一个有理数
We can identify each rational number
21.4.实数的完备性¶
21.4. The Completeness of the Real Numbers¶
我们构造了实数来填补有理数的空白。我们怎么知道我们已经得到了所有的实数?也许我们需要使用实数的柯西序列构造更多的数字?下一个定理告诉我们,相反,没有必要以这种方式进一步扩展实数。
We constructed the real numbers to fill in the gaps in the rationals. How do we know that we have got them all? Perhaps we need to construct even more numbers, using Cauchy sequences of reals? The next theorem tells us that, on the contrary, there is no need to extend the reals any further in this way.
定义。让
Definition. Let
定义。一个序列
Definition. A sequence
定理。每个柯西实数序列都收敛。
Theorem. Every Cauchy sequence of real numbers converges.
该定理的表述通常表示为实数是完备的。粗略地说,它表示无论你在哪里寻找实数,你都一定会找到一个实数。这里有一个类似的原理。
The statement of the theorem is often expressed by saying that the real numbers are complete. Roughly, it says that everywhere you look for a real number, you are bound to find one. Here is a similar principle.
定义。一个元素
Definition. An element
定理。设
Theorem. Let
有理数不具备这种性质:如果我们设定
The rational numbers do not have this property: if we set
一个基本定理是,实数的特征在于它们是完全有序域,因此每个实数
It is a fundamental theorem that the real numbers are characterized exactly by the property that they are a complete ordered field, such that every real number
21.5.另一种构造方法¶
21.5. An Alternative Construction¶
许多资料使用实数的另一种构造方法,将它们视为Dedekind 截断。Dedekind 截断是有序对
Many sources use an alternative construction of the reals, taking them instead to be Dedekind cuts. A Dedekind cut is an ordered pair
每一个有理数
要么 或者 。Every rational number
is in either or .每个
小于每一个 。Each
is less than every .没有最大的元素
。There is no greatest element of
. 和 均非空。 and are both nonempty.
前两个属性说明了为什么我们称这对为“切割”。集合
The first two properties show why we call this pair a “cut.” The set
我们可以定义
We can define
有些作者更喜欢这种构造而不是柯西序列构造,因为它避免了取集合的商,从而消除了证明算术运算尊重等价性的复杂性。其他人更喜欢柯西序列,因为它们提供了更清晰的近似概念:如果一个实数
Some authors prefer this construction to the Cauchy sequence construction because it avoids taking the quotient of a set, and thus removes the complication of showing that arithmetic operations respect equivalence. Others prefer Cauchy sequences since they provide a clearer notion of approximation: if a real number
不过,对于大多数数学家来说,大多数时候,这种差异并不重要。这两种构造都创建了完整的线性有序域,在某种意义上,它们创建的是相同的完整的线性有序域。严格地说,柯西实数集不等于戴德金实数集,因为一个由有理柯西序列的等价类组成,另一个由有理数集对组成。但这两个集合之间存在一个双射,可以保留域属性。也就是说,存在一个双射
For most mathematicians most of the time, though, the difference is immaterial. Both constructions create complete linear ordered fields, and in a certain sense, they create the same complete linear ordered field. Strictly speaking, the set of Cauchy reals is not equal to the set of Dedekind reals, since one consists of equivalence classes of rational Cauchy sequences and one consists of pairs of sets of rationals. But there is a bijection between the two sets that preserves the field properties. That is, there is a bijection
我们称这两个构造是同构的,并且函数
We say that the two constructions are isomorphic, and that the function
21.6.练习¶
21.6. Exercises¶
证明第 21.2 节中定义的整数加法是交换律和结合律。
Show that addition for the integers, as defined in Section 21.2, is commutative and associative.
根据第 21.2 节中的整数构造证明
对于每个整数 。Show from the construction of the integers in Section 21.2 that
for every integer .定义整数减法
并证明 对于每一对整数 和 。Define subtraction for the integers by
, and show that for every pair of integers and .定义整数的乘法,首先在底层表示上定义它,然后表明该运算遵循等价关系。
Define multiplication for the integers, by first defining it on the underlying representation and then showing that the operation respects the equivalence relation.
证明每个柯西序列都是有界的:即若
是柯西,有理 使得 面向所有人 . 提示:尝试让 。Show that every Cauchy sequence is bounded: that is, if
is Cauchy, there is some rational such that for all . Hint: try letting .让
和 是柯西序列。定义 和 。表明
是柯西的。也就是说,对于任意 ,表明存在一个 对于所有 , 。表明
是柯西。除了三角不等式之外,你会发现前面的练习很有用。
Let
and be Cauchy sequences. Define and .Show that
is Cauchy. That is, for arbitrary , show that there exists an such that for all , .Show that
is Cauchy. In addition to the triangle inequality, you will find the previous exercise useful.
这两部分证明了柯西序列的加法满足等价性。
表明如果
是柯西序列,并且 , 然后 。使用该问题的第一部分,证明如果
是柯西序列, , 和 , 然后 。你可以利用实数加法可交换的事实。
These two parts show that addition of Cauchy sequences respects equivalence.
Show that if
are Cauchy sequences and , then .Using the first part of this problem, show that if
are Cauchy sequences, , and , then . You can use the fact that addition on the real numbers is commutative.
表明如果
和 是戴德金割集,那么 也是Dedekind切割。Show that if
and are Dedekind cuts, then is also a Dedekind cut.
22.无限¶
22. The Infinite¶
22.1.等量性¶
22.1. Equinumerosity¶
记得在第 20 章中我们定义过,对于每个自然数
Remember that in Chapter 20 we defined, for each natural number
如果
If
定义。两个集合
Definition. Two sets
在这个阶段,说
At this stage, saying that
以下定理本质上说,等数性是一种等价关系。(需要注意的是,到目前为止,我们只讨论了集合之间的关系,所有集合的集合本身并不是一个集合。)
The following theorem says, essentially, that equinumerosity is an equivalence relation. (The caveat is that so far we have spoke only of relations between sets, and the collection of all sets is not itself a set.)
命题。设
Proposition. Let
。 .如果
, 然后 。If
, then .如果
和 然后 。If
and then .
证明留作练习。
The proof is left as an exercise.
22.2.可数无限集¶
22.2. Countably Infinite Sets¶
自然数集,
The set of natural numbers,
定义。如果一个集合等数于
Definition. A set is said to be countably infinite if it is equinumerous with
由于恒等映射
Since the identity map
“可数无限”这个术语意在引起共鸣。假设
The term “countably infinite” is meant to be evocative. Suppose
有了这个定义,我们自然会想知道我们最喜欢的哪些集合是可数的。整数集
With this definition in hand, it is natural to wonder which of our favorite sets are countable. Is the set of integers
定理。整数集,
Theorem. The set of integers,
证明。我们需要证明
Proof. We need to show that there exists a bijection between
我们主张
We claim that
看到这一点
To see that
我们现在将建立一个定理库,用来证明各种集合都是可数的。
We will now build up an arsenal of theorems that we can use to show that various sets are countable.
定理。一个集合
Theorem. A set
证明。对于正向,假设
Proof. For the forward direction, suppose
换句话说,
In other words,
在另一个方向上,如果
In the other direction, if
准确地说,函数
To be precise, the function
我们只需要检查
We only need to check that
以类似于我们证明整数可数的方式,我们可以证明以下内容:
In a manner similar to the way we proved that the integers are countable, we can prove the following:
定理。如果
Theorem. If
证明。假设
Proof. Suppose
不难证明
It is not hard to show that
直观地看,如果
Intuitively, if
接下来的两个定理也很有帮助。第一个定理说,为了证明一个集合
The next two theorems are also helpful. The first says that to show that a set
定理。如果
Theorem. If
证明。如果
Proof. If
定理。如果
Theorem. If
证明。假设
Proof. Assuming
推论。如果
Corollary. If
证明。函数
Proof. The function
记住
Remember that
定理。
Theorem.
证明。列举元素如下:
Proof. Enumerate the elements as follows:
如果你把这些对看作
If you think of the pairs as coordinates in the
就证明而言,上述非正式描述和相关图表非常令人信服。可以描述
As far as proofs go, the informal description above and the associated diagram are perfectly compelling. It is possible to describe a bijection between
先前的定理有许多有趣的推论。
The previous theorem has a number of interesting consequences.
定理。如果
Theorem. If
证明。如果
Proof. If
定理。有理数集,
Theorem. The set of rational numbers,
证明。根据前面的定理,我们知道
Proof. By the previous theorem, we know that
由于
Since every element of
定理。假设
Theorem. Suppose that
证明。记住,我们可以确定
Proof. Remember that we can identify the set of
定理。设
Theorem. Let
证明。假设每个
Proof. Suppose for each
定理。假设
Theorem. Suppose that
证明。元素的有限序列集
Proof. The set of finite sequences of elements of
请注意,所有字母数字字符和标点符号的集合(例如,表示为所有 ASCII 字符的集合)是有限的。结合最后一个定理,这意味着英语中只有可数的句子(实际上,任何句子由从任何可数库存中选择的有限符号序列表示的语言中也只有可数的句子)。
Notice that the set of all alphanumeric characters and punctuation (say, represented as the set of all ASCII characters) is finite. Together with the last theorem, this implies that there are only countably many sentences in the English language (and, indeed, any language in which sentences are represented by finite sequences of symbols, chosen from any countable stock).
在这个阶段,似乎一切都是可数的。在下一节中,我们将看到事实并非如此:实数集
At this stage, it might seem as though everything is countable. In the next section, we will see that this is not the case: the set of real numbers,
22.3.康托定理¶
22.3. Cantor’s Theorem¶
一套
A set
定理。实数集是不可数的。
Theorem. The set of real numbers is uncountable.
证明。记住
Proof. Remember that
回想一下,每个实数
Recall that every real number
(请注意
(Notice that
因此,我们可以写
As a result, we can write
…
…
(我们使用上标,
(We use superscripts,
我们的目标是证明
Our goal is to show that
定义实数
The define the real number
(我们选择数字
(We chose the digits
这个引人注目的证明被称为“对角化论证”。我们试图构造一个具有特定属性的实数,即它不在
This remarkable proof is known as a “diagonalization argument.” We are trying to construct a real number with a certain property, namely, that it is not in the range of
下面提供了不可数集的另一个例子。
The following provides another example of an uncountable set.
定理。自然数的幂集,
Theorem. The power set of the natural numbers,
证明。设
Proof. Let
换句话说,对于每个自然数,
In words, for every natural number,
我们也可以将其视为对角化论证:绘制一个表格,行和列均由自然数索引,其中
We can also view this as a diagonalization argument: draw a table with rows and columns indexed by the natural numbers, where the entry in the
事实上,完全相同的论证可以得出以下结论:
In fact, exactly the same argument yields the following:
定理。对于每个集合
Theorem. For every set
证明。如上所述,如果
Proof. As above, if
这表明无穷级数是无穷的。例如,在序列
This shows that there is an endless hierarchy of infinities. For example, in the sequence
22.4.有限性的另一种定义¶
22.4. An Alternative Definition of Finiteness¶
无限与有限的区别之一是,无限集可以与其本身的真子集大小相同。例如,自然数、偶数集和完全平方集都是等数集,尽管后两者严格包含在自然数中。
One thing that distinguishes the infinite from the finite is that an infinite set can have the same size as a proper subset of itself. For example, the natural numbers, the set of even numbers, and the set of perfect squares are all equinumerous, even though the latter two are strictly contained among the natural numbers.
19 世纪,数学家理查德·戴德金 (Richard Dedekind) 使用这一奇特的性质来定义有限性。我们可以证明他的定义与我们的定义等价,但证明需要选择公理。
In the nineteenth century, the mathematician Richard Dedekind used this curious property to define what it means to be finite. We can show that his definition is equivalent to ours, but the proof requires the axiom of choice.
定义。集合是
Definition. A set is
定理。一个集合是戴德金无限的当且仅当它是无限的。
Theorem. A set is Dedekind infinite if and only it is infinite.
证明。假设
Proof. Suppose
另一方面,假设
Suppose, on the other hand, that
22.5.康托-伯恩斯坦定理¶
22.5. The Cantor-Bernstein Theorem¶
说
Saying that
定义。对于两个集合
Definition. For two sets
作为练习,我们要求你展示
As an exercise, we ask you to show that
答案是“是”,但证明起来很棘手。这个结果被称为康托-伯恩斯坦定理,我们无需证明即可陈述它。
The answer is “yes,” but the proof is tricky. The result is known as the Cantor-Bernstein Theorem, and we state it without proof.
定理。对于任何集合
Theorem. For any sets
22.6.练习¶
22.6. Exercises¶
证明等量性是自反的、对称的、和传递的。
Show that equinumerosity is reflexive, symmetric, and transitive.
证明函数
是区间之间的双射 和 。Show that the function
is a bijection between the interval and .表明
给出之间的双射 和 。Show that the
gives a bijection between and .定义函数
经过 。这个问题的目的是为了证明 是来自的双射 到 。画一个图来表明哪些对被发送到
。让
. 表明 等于对数 如此一来 , 或者 和 .(利用这一事实 。结论
是单射:找到 和 使得 ,只要找到最大的 使得 , 让 ,并让 。结论
是单射:如果 , 让 和 . 认为 , 所以 和 。
Define a function
by . This goal of this problem is to show that is a bijection from to .Draw a picture indicating which pairs are sent to
.Let
. Show that is equal the number of pairs such that either , or and . (Use the fact that .)Conclude that
is surjective: to find and such that , it suffices to find the largest such that , let , and let .Conclude that
is injective: if , let and . Argue that , and so and .
让
是来自的函数集 到 . 使用对角论证来证明 是不可数的。(请注意,你可以想象一个函数 作为 0 和 1 的无限序列,由下式给出 .因此,给定一个函数 对于每个自然数 ,返回一个由 0 和 1 组成的无限序列,你需要找到一个不在图像中的序列 。Let
be the set of functions from to . Use a diagonal argument to show that is uncountable. (Notice that you can think of a function as an infinite sequence of 0’s and 1’s, given by . So, given a function which, for each natural number , returns an infinite sequence of 0’s and 1’s, you need to find a sequence that is not in the image of .)如果
和 是来自的函数 到 ,说 最终占据主导地位 如果有的话 对于每一个 , 换句话说,从某个时刻开始, 大于 。表明如果
是来自的任意函数序列 到 ,以自然数为指标,则有一个函数 最终主宰每一个 . (提示:构造 因此对于每一个 , 对于每一个 。If
and are functions from to , say that eventually dominates if there is some such that for every , . In other words, from some point on, is bigger than .Show that if
is any sequence of functions from to , indexed by the natural numbers, then there is a function that eventually dominates each . (Hint: construct so that for each , for every .)表明关系
在第 22.5 节中定义是自反的和传递的。Show that the relation
defined in Section 22.5 is reflexive and transitive.
23.公理基础¶
23. Axiomatic Foundations¶
在这最后一章中,我们的故事又回到了原点。我们从符号逻辑开始我们的旅程,使用命题连接词来模拟“与”、“或”、“非”和“蕴含”等逻辑术语。在此基础上,我们添加了一阶逻辑的量词、函数和关系符号。从那里,我们转向集合、函数和关系,它们在现代数学中无处不在;自然数和归纳法;然后是数论、组合学、实数和无穷大等主题。在这里,我们回到符号逻辑,看看它如何用于为所有数学提供形式基础。
In this final chapter, our story comes full circle. We started our journey with symbolic logic, using the propositional connectives to model logical terms like “and,” “or,” “not,” and “implies.” To that we added the quantifiers and function and relation symbols of first-order logic. From there, we moved to sets, functions, and relations, which are ubiquitous in modern mathematics; the natural numbers and induction; and then topics such as number theory, combinatorics, the real numbers, and the infinite. Here we return to symbolic logic, and see how it can be used to provide a formal foundation for all of mathematics.
具体来说,我们将考虑一个公理框架,即Zermelo-Fraenkel 集合论,它是在 20 世纪初引入的。在数学的集合论观点中,每个数学对象都是一个集合。公理断言具有各种属性的集合的存在。从所有集合的集合中,我们挖掘出数学宇宙的常见成员,不仅仅是我们考虑过的各种数字系统,还包括对、有限序列、关系、函数等等。这为我们自第 11 章以来所做的一切提供了理想化的基础。
Specifically, we will consider an axiomatic framework known as Zermelo-Fraenkel set theory, which was introduced early in the twentieth century. In the set-theoretic view of mathematics, every mathematical object is a set. The axioms assert the existence of sets with various properties. From the collection of all sets, we carve out the usual inhabitants of the mathematical universe, not just the various number systems we have considered, but also pairs, finite sequences, relations, functions, and so on. This provides us with an idealized foundation for everything we have done since Chapter 11.
在本章的最后,我们将简要介绍另一个公理框架,即 Lean 所使用的依赖类型理论。我们将看到,它为数学对象和构造提供了另一种视角,但仍然可以与集合论观点相互解释。
At the end of this chapter, we will briefly describe another axiomatic framework, dependent type theory, which is the one used by Lean. We will see that it provides an alternative perspective on mathematical objects and constructions, but one which is nonetheless inter-interpretable with the set-theoretic point of view.
23.1.集合的基本公理¶
23.1. Basic Axioms for Sets¶
集合论的公理用一阶逻辑来表达,对于具有单个二元关系符号的语言,
The axioms of set theory are expressed in first-order logic, for a language with a single binary relation symbol,
下一个公理告诉我们,宇宙中至少有一个有趣的集合,即没有元素的集合。
The next axiom tells us that there is at least one interesting set in the universe, namely, the set with no element.
当然,
Here, of course,
第三条公理告诉我们,给定两个集合
The third axiom tells us that given two sets
这条公理有一个隐秘的用法。这条公理不需要
There is a stealth usage of this axiom lurking nearby. The axiom does not require that
尽管如此,我们永远无法以这种方式形成一个包含两个以上元素的集合。为此,合理的做法是添加一条公理,该公理断言对于每个
Still, we can never form a set with more than two elements in this way. To that end, it would be reasonable to add an axiom that asserts for every
再次证明,使用
Once again, this justifies the use of the
在这个阶段,调用我们在集合的非正式表示中首次引入的一些附加符号将很有用。如果
At this stage, it will be useful to invoke some additional notation that was first introduced in our informal presentation of sets. If
下一个公理断言,对于每个集合
The next axiom asserts that for every set
我们已经开始用基本集合构造填充宇宙。然而,正是下一个公理赋予了集合论非凡的灵活性。严格地说,它不是单个公理,而是一个模式,即由单个模板给出的无限公理系列。该模式旨在证明集合构造器符号的合理性
We have begun to populate the universe with basic set constructions. It is the next axiom, however, that gives set theory its remarkable flexibility. Properly speaking, it is not a single axiom, but a schema, an infinite family of axioms given by a single template. The schema is meant to justify set-builder notation
另一个担忧围绕着罗素悖论,如第 11.1 节所述。任何允许我们定义集合的理论
Another concern centers around Russell’s paradox, as discussed in Section 11.1. Any theory that allows us to define the set
我们想要的公理模式被称为分离,因为我们用它将我们想要的元素从更大的集合中的元素中分离出来。
The axiom schema we want is called separation, because we use it to separate the elements we want from those in a bigger collection.
这里,
Here,
我们可以使用分离公理来简化前面的公理。例如,只要我们知道任何集合
We could use the separation axiom to simplify the previous axioms. For example, as long as we know that any set
这些只是集合论的前六个公理;我们还有四个要学。但这些公理本身就为推理集合、关系和函数提供了基础,就像我们在第 11 章、第 13 章和第 15 章中所做的那样。例如,我们已经定义了并集运算,我们可以定义集合交集
These are only the first six axioms of set theory; we have four more to go. But these axioms alone provide a foundation for reasoning about sets, relations, and functions, as we did in Chapter 11, Chapter 13, and Chapter 15. For example, we have already defined the union operation, and we can define set intersection
我们想定义两个集合之间的二元关系
We would like to define a binary relation between two sets
只要我们能证明存在一个足够大的集合来填充“……”。在下面的练习中,我们要求你证明集合
provided we can prove the existence of a set big enough to fill the “….” In the exercises below, we ask you to show that the set
现在我们可以说一个函数
Now we can say that a function
有了集合、关系和函数,我们就有了做数学的基本基础。现在我们只缺少一些有趣的集合和结构来处理。例如,如果有一组自然数就好了,
With sets, relations, and functions, we have the basic infrastructure we need to do mathematics. All we are missing at this point are some interesting sets and structures to work with. For example, it would be nice to have a set of natural numbers,
23.2.无限公理¶
23.2. The Axiom of Infinity¶
利用我们目前掌握的公理,我们可以形成许多有限集,首先是
With the axioms we have so far, we can form lots of finite sets, starting with
但迄今为止的公理还不允许我们定义比这些更有趣的集合。特别是,没有一条公理能给出无限集。所以我们需要进一步的公理来告诉我们这样的集合是存在的。
But the axioms so far do not allow us to define sets that are more interesting than these. In particular, none of the axioms gives us an infinite set. So we need a further axiom to tell us that such a set exists.
还记得在第 17 章中,我们将自然数描述为具有独特元素的集合,
Remember that in Chapter 17 we characterized the natural numbers as a set with a distinguished element,
更清楚的写法如下:
It is more perspicuous to write them as follows:
一般来说,
In general,
回想一下第 17 章,我们可以将自然数集描述如下:
Recall from Chapter 17 that we can characterize the set of natural numbers as follows:
有一个元素
并且有一个注入函数 ,并附加以下属性 对于任意 在 。There is an element
and there is an injective function , with the additional property that for any in .该套装
满足归纳原理:如果 是 包含 并关闭 (即,每当 位于 ,所以 ), 然后 。The set
satisfies the principle of induction: if is a subset of that contains and is closed under (that is, whenever is in , so is ), then .
我们已经确定了
We have already settled on the definitions of
说一套
Say a set
让
Let
这里
Here
很快,
It quickly follows that
更有趣的是
The more interesting point is that
总而言之,我们证明了一个集合的存在,它包含
To summarize, then, we have proved the existence of a set that contains
现在只缺少了一块拼图。从定义中可以清楚地看出
There is only one piece of the puzzle missing. It is clear from the definition that
引理:每个自然数都是可传递的。
Lemma. Every natural number is transitive.
证明。通过对自然数进行归纳。显然,
Proof. By induction on the natural numbers. Clearly,
下一个引理表明,在传递集上,并集的作用类似于前任运算。
The next lemma shows that, on transitive sets, union acts like the predecessor operation.
引理。如果
Lemma. If
证明。假设
Proof. Suppose
相反,假设
Conversely, suppose
定理。
Theorem.
证明。假设
Proof. Suppose
这样,我们就可以开始了。虽然我们不会在这里介绍细节,但使用归纳原理,我们可以证明递归定义原理的合理性。然后,我们可以继续定义算术的基本运算并推导它们的属性,如第 17 章中所做的那样。我们可以继续定义整数、有理数和实数,如第 21章中所述,并开发数论和组合学等主题,如第 19 章和第 20 章中所述。事实上,似乎任何合理的数学分支都可以在公理集合论的基础上正式发展。例如,存在与大集合有关的陷阱:例如,正如假设存在一个由所有集合组成的集合是不一致的一样,也不存在所有偏序或所有群的集合。因此,在解释某些数学主张时,在某些情况下必须小心,将其限制在足够大的此类对象集合上。但这几乎只相当于仔细的记账,而且值得注意的是,在大多数情况下,集合论的公理足够灵活和强大,可以证明大多数普通的数学构造。
With that, we are off and running. Although we will not present the details here, using the principle of induction we can justify the principle of recursive definition. We can then go on to define the basic operations of arithmetic and derive their properties, as done in Chapter 17. We can go on to define the integers, the rational numbers, and the real numbers, as described in Chapter Chapter 21, and to develop subjects like number theory and combinatorics, as described in Chapters Chapter 19 and Chapter 20. In fact, it seems that any reasonable branch of mathematics can be developed formally on the basis of axiomatic set theory. There are pitfalls, for example, having to do with large collections: for example, just as it is inconsistent to postulate the existence of a set of all sets, in the same way, there is no collection of all partial orders, or all groups. So when interpreting some mathematical claims, care has to be taken in some cases to restrict to sufficiently large collections of such objects. But this rarely amounts to more than careful bookkeeping, and it is a remarkable fact that, for the most part, the axioms of set theory are flexible and powerful enough to justify most ordinary mathematical constructions.
23.3.剩余的公理¶
23.3. The Remaining Axioms¶
我们已经看到的七条公理非常强大,足以代表大部分数学。我们在这里讨论 Zermelo-Fraenkel 集合论的其余公理。
The seven axioms we have seen are quite powerful, and suffice to represent large portions of mathematics. We discuss the remaining axioms of Zermelo-Fraenkel set theory here.
到目前为止,我们所见过的所有公理都没有排除集合
So far, none of the axioms we have seen rule out the possibility that a set
这条公理说,如果
The axiom says that if
如果我们将基础公理应用于集合
If we apply the axiom of foundation to the set
上一节列出的公理讲述了集合是如何形成的:我们从空集开始,不断应用幂集、并集和分离等构造来构建更多集合。集合论者经常将集合的层次想象成一个大 V 字,空集位于最底层,任何更高级别的集合都包含出现在较低级别的集合作为其元素。从精确的意义上讲(我们不会在这里详细说明),基础公理表明每个集合都是以这种方式产生的。
The axioms listed in the previous section tell a story of how sets come to be: we start with the empty set, and keep applying constructions like power set, union, and separation, to build more sets. Set theorists often imagine the hierarchy of sets as forming a big V, with the empty set at the bottom and a set at any higher level comprising, as its elements, sets that appear in levels below. In a precise sense (which we will not spell out here), the axiom of foundation says that every set arises in such a way.
现在考虑以下集合序列:
Now consider the following sequence of sets:
这与我们迄今为止看到的所有公理一致,即数学宇宙中的每个集合都是其中一个元素。这仍然给我们很多集合,但是,既然我们已经描述了该序列,我们也可以想象一个包含所有集合的集合:
It is consistent with all the axioms we have seen so far that every set in the mathematical universe is an element of one of these. That still gives us a lot of sets, but, since we have described that sequence, we can just as well imagine a set that contains all of them:
下列公理暗示了这样一个集合的存在。
The following axiom implies the existence of such a set.
和分离公理一样,这个公理实际上是一个模式,也就是说,每个公式都有一个单独的公理
Like the axiom of separation, this axiom is really a schema, which is to say, a separate axiom for each formula
到目前为止,我们列出的九条公理构成了所谓的Zermelo-Fraenkel 集合论。还有一个附加公理,即选择公理,由于历史原因,它通常被单独列出:它曾被认为是有争议的,在早期,数学家认为跟踪公理是否在证明中实际使用很重要。有许多等效的表述,但这个是最直接的表述之一。
The nine axioms we have listed so far comprise what is known as Zermelo-Fraenkel Set Theory. There is on additional axiom, the axiom of choice, which is usually listed separately for historical reasons: it was once considered controversial, and in the early days, mathematicians considered it important to keep track of whether the axiom was actually used in a proof. There are many equivalent formulations, but this one is one of the most straightforward.
公理说,对于任何集合
The axiom says that for any collection
总结一下,策梅洛-弗兰克尔集合论的公理和选择公理如下:
To summarize, then, the axioms of Zermelo-Fraenkel Set Theory with the axiom of choice are as follows:
外延性:
Extensionality:
空集:
Empty set:
搭配:
Pairing:
联盟:
Union:
功率设置:
Power set:
分离:
Separation:
无穷大:
Infinity:
基础:
Foundation:
替代品:
Replacement:
选择:
Choice:
23.4.类型理论¶
23.4. Type Theory¶
作为数学的基础,策梅洛-弗兰克尔集合论颇具吸引力。其底层逻辑——一阶逻辑,为量词和逻辑联结词提供了基本的逻辑框架。除此之外,该理论还描述了一个单一的、直观自然的概念,即一组元素。这些公理非常合理。几乎所有现代数学都可以简化为如此简单的术语,这真是令人惊叹。
As a foundation for mathematics, Zermelo-Fraenkel set theory is appealing. The underlying logic, first-order logic, provides the basic logical framework for quantifiers and the logical connectives. On top of that, the theory describes a single, intuitively natural concept, that of a set of elements. The axioms are plausible eminently reasonable. It is remarkable that virtually all of modern mathematics can be reduced to such simple terms.
然而,还有其他基础可供选择。这些基础往往在很大程度上可以与集合论相互解释。毕竟,集合论语言现在在日常数学中无处不在,所以任何合理的基础都应该能够理解这种语言。另一方面,我们已经注意到集合论具有极强的表达能力和鲁棒性,因此其他基础方法通常可以用集合论术语来理解也就不足为奇了。
There are other foundations on offer, however. These tend to be largely inter-interpretable with set theory. After all, set-theoretic language is now ubiquitous in everyday mathematics, so any reasonable foundation should be able to make sense of such language. On the other hand, we have already noted that set theory is remarkably expressive and robust, and so it should not be surprising that other foundational approaches can often be understood in set-theoretic terms.
依赖类型理论尤其如此,它是 Lean 定理证明器的基础。类型理论的语法比集合论的语法更复杂。在集合论中,只有一种对象;正式地说,一切都是集合。相比之下,在类型理论中,Lean 中每个格式正确的表达式都有一个类型,并且有丰富的定义类型的词汇表。
This is, in particular, true of dependent type theory, which is the basis of the Lean theorem prover. The syntax of type theory is more complicated than that of set theory. In set theory, there is only one kind of object; officially, everything is a set. In contrast, in type theory, every well-formed expression in Lean has a type, and there is a rich vocabulary of defining types.
事实上,精益是基于一种被称为归纳构造演算的公理框架版本,它提供了以下所有内容:
In fact, Lean is based on a version of an axiomatic framework known as the Calculus of Inductive Constructions, which provides all of the following:
类型宇宙的层次结构,,,,,… 以及特殊类型。表达式缩写,并且说可以解释为说是一种数据类型。类型是命题的类型。
Type 0Type 1Type 2PropTypeType 0T : TypeTPropA hierarchy of type universes,
Type 0,Type 1,Type 2, … and a special typeProp. The expressionTypeabbreviatesType 0, and sayingT : Typecan be interpreted as saying thatTis a datatype. The typePropis the type of propositions.依赖函数类型 。此类型的元素是将任何类型元素映射到类型元素的函数。输出类型取决于输入类型,这就是函数“依赖”的原因。在输出类型不依赖于输入的情况下,我们有简单的函数类型。
Π x : A, B xfaAf aB aA → BDependent function types
Π x : A, B x. An elementfof this type is a function which maps any elementaof typeAto an elementf aof typeB a. The fact that the type of the output depends on the type of the input is what makes the function “dependent.” In the case where the output type does not depend on the input, we have the simple function typeA → B.归纳类型,如自然数,由构造函数指定,如零和后继。每种此类类型都带有归纳和递归原理。
Inductive types, like the natural numbers, specified by constructors, like zero and successor. Each such type comes with principles of induction and recursion.
这些构造既解释了断言(即命题)的底层逻辑,也解释了宇宙的对象(普通类型的元素)。
These constructions account for both the underlying logic of assertions (that is, the propositions) as well as the objects of the universe, which are elements of the ordinary types.
在集合论中解释类型论很简单,因为我们可以将每种类型视为一个集合。类型宇宙只是集合的大集合,依赖函数类型和归纳类型可以用集合论构造来解释。我们可以将集合Prop视为
It is straightforward to interpret type theory in set theory, since we can view each type as a set. The type universes are simply large collections of sets, and dependent function types and inductive types can be explained in terms of set-theoretic constructions. We can view Prop as the set
考虑到最后一个事实,为什么不直接使用集合论而不是类型论来进行交互式定理证明呢?一些交互式定理证明器就是这样做的。但类型论有一些优势:
Given this last fact, why not just use set theory instead of type theory for interactive theorem proving? Some interactive theorem provers do just that. But type theory has some advantages:
表达式的形成规则如此严格,使得系统更容易识别印刷错误并提供有用的反馈。在类型论中,如果
f具有类型,则只能应用于自然数,并且如果参数的类型错误,定理证明器可以标记错误。在集合论中,任何东西都可以应用于任何东西,无论这样做是否真的有意义。ℕ → ℕThe fact that the rules for forming expressions are so rigid makes it easier for the system to recognize typographical errors and provide useful feedback. In type theory, if
fhas typeℕ → ℕit can be applied only to a natural number, and a theorem prover can flag an error if the argument has the wrong type. In set theory, anything can be applied to anything, whether or not doing so really makes sense.同样,由于形成表达式的规则非常严格,系统可以从表达式的组成部分推断出有用的信息,而集合论则要求我们将这些信息明确化。例如,如上所述,定理证明器可以推断出中的
f变量应该具有类型,并且结果表达式再次具有类型。在集合论中,xf xℕℕ 必须以明确的假设来表述,并且 那么就是一个定理。Again, because the rules for forming expressions are so rigid, the system can infer useful information from the components of an expression, whereas set theory would require us to make such information explicit. For example, with
fas above, a theorem prover can infer that a variablexinf xshould have typeℕ, and that the resulting expression again has typeℕ. In set theory, has to be stated as an explicit hypothesis, and is then a theorem.通过将命题编码为某些类型,我们可以使用相同的语言来定义数学对象和编写数学证明。例如,我们可以将函数应用于参数,就像我们将定理应用于某些假设一样。
By encoding propositions as certain kinds of types, we can use the same language for defining mathematical objects and writing mathematical proofs. For example, we can apply a function to an argument in the same way we apply a theorem to some hypotheses.
依赖类型理论中足够纯粹的部分中的表达式具有计算解释,因此,例如,逻辑框架告诉我们如何在给定其定义的情况下评估阶乘函数。在集合论中,计算解释是在事后独立指定的。
Expressions in a sufficiently pure part of dependent type theory have a computational interpretation, so, for example, the logical framework tells us how to evaluate the factorial function, given its definition. In set theory, the computational interpretation is specified independently, after the fact.
这些事实让我们回想起我们在第 1 章中提出的关注点分离:不同的公理基础提供不同的理想化数学活动描述,并可设计用于不同的目的。如果您想要一个干净、简单的理论来解释绝大多数数学证明,集合论是您的不二之选。如果您正在寻找一个以计算为中心或以函数而不是集合的概念为基础的基础,各种类型论都有其魅力。对于交互式定理证明,关于实现和可用性的实际问题就会发挥作用。重要的是要认识到,所有这些理想化的描述的共同点是,它们都是为模拟数学语言和证明的重要方面而设计的。我们的目标是帮助您反思数学语言和证明的那些赋予数学特殊性质的特征,并帮助您更好地理解它们的工作原理。
These facts hark back to the separation of concerns that we raised in Chapter 1: different axiomatic foundations provide different idealized descriptions of mathematical activity, and can be designed to serve different purposes. If you want a clean, simple theory that accounts for the vast majority of mathematical proof, set theory is hard to beat. If you are looking for a foundation that makes computation central or takes the notion of a function rather than a set as basic, various flavors of type theory have their charms. For interactive theorem proving, pragmatic issues regarding implementation and usability come into play. What is important to recognize is that what all these idealized descriptions have in common is that they are all designed to model important aspects of mathematical language and proof. Our goal here has been to help you reflect on those features of mathematical language and proof that give mathematics its special character, and to help you better understand how they work.
23.5.练习¶
23.5. Exercises¶
使用类似罗素悖论的论点来表明不存在“所有集合的集合”,也就是说,不存在包含其他所有集合作为元素的集合。
Use an argument similar Russell’s paradox to show that there is no “set of all sets,” that is, there is no set that contains every other set as an element.
认为
是一个非空集合,包含一个元素 . 利用分离公理证明集合 存在。(记住某物是 如果它是 。Suppose
is a nonempty set, say, containing an element . Use the axiom of separation to show that the set exists. (Remember that something is an element of if it is an element of every element of .)证明第 23.1 节中的主张,即
是...的一个元素 。Justify the claim in Section 23.1 that every element of
is an element of .给定一个集合
和一个函数 ,利用集合论的公理来证明 。Given a set
and a function , use the axioms of set theory to prove the existence of .
24.附录:自然演绎规则¶
24. Appendix: Natural Deduction Rules¶
含义:
Implication:
连词:
Conjunction:
否定:
Negation:
析取:
Disjunction:
真与假:
Truth and falsity:
双重含义:
Bi-implication:
归谬法(反证法):
Reductio ad absurdum (proof by contradiction):
全称量词:
The universal quantifier:
在介绍规则中,
In the introduction rule,
存在量词:
The existential quantifier:
在介绍规则中,
In the introduction rule,
平等:
Equality:
严格来说,只有
Strictly speaking, only